原創|行業資訊|編輯:鄭恭琳|2015-08-31 10:49:24.000|閱讀 530 次
概述:有關C#中15大被隱藏的頂級功能的第一個帖子是出現在Automate The Planet上。下面列出程序員在C#編程生涯中最喜歡的被隱藏的C#功能,包括完整的解釋和代碼示例。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
我不確定這些能不能被當作C#中隱藏的功能,因為它們沒有被公開,因此應該謹慎使用。沒有一個關于原因的文檔,也許是因為沒有經過充分的測試。然而,它們由Visual Studio編輯器著色,并且識別為官方關鍵字。
你可以通過使用__makeref關鍵字創建變量的類型引用。由類型引用表示的變量的原始類型可以使用__reftype關鍵字提取。最后,該值可以從使用__refvalue關鍵字從TypedReference獲得。__arglist與關鍵字params具有相似的行為-可以訪問參數列表。
int i = 21; TypedReference tr = __makeref(i); Type t = __reftype(tr); Console.WriteLine(t.ToString()); int rv = __refvalue( tr,int); Console.WriteLine(rv); ArglistTest.DisplayNumbersOnConsole(__arglist(1, 2, 3, 5, 6));
要使用__arglist,你需要ArglistTest類。
public static class ArglistTest { public static void DisplayNumbersOnConsole(__arglist) { ArgIterator ai = new ArgIterator(__arglist); while (ai.GetRemainingCount() > 0) { TypedReference tr = ai.GetNextArg(); Console.WriteLine(TypedReference.ToObject(tr)); } } }
從第一個可選參數開始備注ArgIterator對象列舉的參數列表,這是專門為了與C/ C ++編程語言的使用提供的。
相關文檔: 和
獲取此環境中定義的換行符的字符串。
Console.WriteLine("NewLine: {0} first line{0} second line{0} third line", Environment.NewLine);
官方文檔:
表示在代碼中特定點捕獲的異常。你可以使用ExceptionDispatchInfo.Throw方法,可以在System.Runtime.ExceptionServices命名空間中找到。這種方法可用于引發異常和保留原始堆棧跟蹤。
ExceptionDispatchInfo possibleException = null; try { int.Parse("a"); } catch (FormatException ex) { possibleException = ExceptionDispatchInfo.Capture(ex); } if (possibleException != null) { possibleException.Throw(); }
捕獲到的異常可以通過另一種方法再次被拋出,甚至在另一個線程拋出。
官方文檔:
如果要退出程序而無需調用任何finally塊或終結器那么使用FailFast。
string s = Console.ReadLine(); try { int i = int.Parse(s); if (i == 42) Environment.FailFast("Special number entered"); } finally { Console.WriteLine("Program complete."); }
如果i=42,那么finally模塊則不被執行。
官方文檔:
Debug.Assert-檢查條件;如果條件為假,輸出的消息并顯示調用堆棧的消息框。
Debug.Assert(1 == 0, "The numbers are not equal! Oh my god!");
如果斷言在調試模式失敗,則顯示包含指定的消息的警告,如下:
Debug.WriteIf–如果條件為真,寫關于偵聽器集中的跟蹤監聽器調試信息。
Debug.WriteLineIf(1 == 1, "This message is going to be displayed in the Debug output! =)");
Debug.Indent/Debug.Unindent–把當前entLevel加一。
Debug.WriteLine("What are ingredients to bake a cake?"); Debug.Indent(); Debug.WriteLine("1. 1 cup (2 sticks) butter, at room temperature."); Debug.WriteLine("2 cups sugar"); Debug.WriteLine("3 cups sifted self-rising flour"); Debug.WriteLine("4 eggs"); Debug.WriteLine("1 cup milk"); Debug.WriteLine("1 teaspoon pure vanilla extract"); Debug.Unindent(); Debug.WriteLine("End of list");
如果要顯示在調試輸出窗口的各組分,可以使用上面的代碼。
官方文檔:, ,
我不確定是否可以把它歸類到C#中被隱藏的功能,因為在TPL (Task Parallel Library)經常用到。然而,我把它放這兒是因為我非常喜歡在多線程應用程序中用到它。
Parallel.For-執行迭代可以并行的for循環。
int[] nums = Enumerable.Range(0, 1000000).ToArray(); long total = 0; // Use type parameter to make subtotal a long, not an int Parallel.For<long>(0, nums.Length, () => 0, (j, loop, subtotal) => { subtotal += nums[j]; return subtotal; }, (x) => Interlocked.Add(ref total, x) ); Console.WriteLine("The total is {0:N0}", total);
Interlocked.Add方法新增兩個整數,并將第一個整數替換為它們的和。
Parallel.Foreach-執行foreach操作,其中迭代可以并行運行。
int[] nums = Enumerable.Range(0, 1000000).ToArray(); long total = 0; Parallel.ForEach<int, long>(nums, // source collection () => 0, // method to initialize the local variable (j, loop, subtotal) => // method invoked by the loop on each iteration { subtotal += j; //modify local variable return subtotal; // value to be passed to next iteration }, // Method to be executed when each partition has completed. // finalResult is the final value of subtotal for a particular partition. (finalResult) => Interlocked.Add(ref total, finalResult)); Console.WriteLine("The total from Parallel.ForEach is {0:N0}", total);
官方文檔:和
返回一個指示指定數字是否計算為負或正無窮大的值。
Console.WriteLine("IsInfinity(3.0 / 0) == {0}.", Double.IsInfinity(3.0 / 0) ? "true" : "false");
官方文檔:
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn