原創(chuàng)|行業(yè)資訊|編輯:鄭恭琳|2015-08-31 10:49:24.000|閱讀 543 次
概述:有關(guān)C#中15大被隱藏的頂級功能的第一個(gè)帖子是出現(xiàn)在Automate The Planet上。下面列出程序員在C#編程生涯中最喜歡的被隱藏的C#功能,包括完整的解釋和代碼示例。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
我不確定這些能不能被當(dāng)作C#中隱藏的功能,因?yàn)樗鼈儧]有被公開,因此應(yīng)該謹(jǐn)慎使用。沒有一個(gè)關(guān)于原因的文檔,也許是因?yàn)闆]有經(jīng)過充分的測試。然而,它們由Visual Studio編輯器著色,并且識別為官方關(guān)鍵字。
你可以通過使用__makeref關(guān)鍵字創(chuàng)建變量的類型引用。由類型引用表示的變量的原始類型可以使用__reftype關(guān)鍵字提取。最后,該值可以從使用__refvalue關(guān)鍵字從TypedReference獲得。__arglist與關(guān)鍵字params具有相似的行為-可以訪問參數(shù)列表。
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));
}
}
}
從第一個(gè)可選參數(shù)開始備注ArgIterator對象列舉的參數(shù)列表,這是專門為了與C/ C ++編程語言的使用提供的。
相關(guān)文檔: 和
獲取此環(huán)境中定義的換行符的字符串。
Console.WriteLine("NewLine: {0} first line{0} second line{0} third line", Environment.NewLine);
官方文檔:
表示在代碼中特定點(diǎn)捕獲的異常。你可以使用ExceptionDispatchInfo.Throw方法,可以在System.Runtime.ExceptionServices命名空間中找到。這種方法可用于引發(fā)異常和保留原始堆棧跟蹤。
ExceptionDispatchInfo possibleException = null;
try
{
int.Parse("a");
}
catch (FormatException ex)
{
possibleException = ExceptionDispatchInfo.Capture(ex);
}
if (possibleException != null)
{
possibleException.Throw();
}
捕獲到的異常可以通過另一種方法再次被拋出,甚至在另一個(gè)線程拋出。
官方文檔:
如果要退出程序而無需調(diào)用任何finally塊或終結(jié)器那么使用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模塊則不被執(zhí)行。
官方文檔:
Debug.Assert-檢查條件;如果條件為假,輸出的消息并顯示調(diào)用堆棧的消息框。
Debug.Assert(1 == 0, "The numbers are not equal! Oh my god!");
如果斷言在調(diào)試模式失敗,則顯示包含指定的消息的警告,如下:

Debug.WriteIf–如果條件為真,寫關(guān)于偵聽器集中的跟蹤監(jiān)聽器調(diào)試信息。
Debug.WriteLineIf(1 == 1, "This message is going to be displayed in the Debug output! =)");
Debug.Indent/Debug.Unindent–把當(dāng)前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");
如果要顯示在調(diào)試輸出窗口的各組分,可以使用上面的代碼。

官方文檔:, ,
我不確定是否可以把它歸類到C#中被隱藏的功能,因?yàn)樵赥PL (Task Parallel Library)經(jīng)常用到。然而,我把它放這兒是因?yàn)槲曳浅O矚g在多線程應(yīng)用程序中用到它。
Parallel.For-執(zhí)行迭代可以并行的for循環(huán)。
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方法新增兩個(gè)整數(shù),并將第一個(gè)整數(shù)替換為它們的和。
Parallel.Foreach-執(zhí)行foreach操作,其中迭代可以并行運(yùn)行。
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);
官方文檔:和
返回一個(gè)指示指定數(shù)字是否計(jì)算為負(fù)或正無窮大的值。
Console.WriteLine("IsInfinity(3.0 / 0) == {0}.", Double.IsInfinity(3.0 / 0) ? "true" : "false");
官方文檔:
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@ke049m.cn