9.未公开的C#关键字__arglist __reftype __makeref __refvalue
我不确定这些能不能被当作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 ++编程语言的使用提供的。
相关文档: 和10. Environment.NewLine
获取此环境中定义的换行符的字符串。Console.WriteLine("NewLine: {0} first line{0} second line{0} third line", Environment.NewLine);
官方文档:
11. ExceptionDispatchInfo
表示在代码中特定点捕获的异常。你可以使用ExceptionDispatchInfo.Throw方法,可以在System.Runtime.ExceptionServices命名空间中找到。这种方法可用于引发异常和保留原始堆栈跟踪。ExceptionDispatchInfo possibleException = null;try{ int.Parse("a");}catch (FormatException ex){ possibleException = ExceptionDispatchInfo.Capture(ex);}if (possibleException != null){ possibleException.Throw();}
捕获到的异常可以通过另一种方法再次被抛出,甚至在另一个线程抛出。
官方文档:12. Environment.FailFast()
如果要退出程序而无需调用任何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模块则不被执行。
官方文档:13. Debug.Assert & Debug.WriteIf & Debug.Indent
Debug.Assert-检查条件;如果条件为假,输出的消息并显示调用堆栈的消息框。Debug.Assert(1 == 0, "The numbers are not equal! Oh my god!");
如果断言在调试模式失败,则显示包含指定的消息的警告,如下:
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");
如果要显示在调试输出窗口的各组分,可以使用上面的代码。
14. Parallel.For & Parallel.Foreach
我不确定是否可以把它归类到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 intParallel.For(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(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);
官方文档: 和
15. IsInfinity
返回一个指示指定数字是否计算为负或正无穷大的值。Console.WriteLine("IsInfinity(3.0 / 0) == {0}.", Double.IsInfinity(3.0 / 0) ? "true" : "false");
官方文档:
转自: