提供3000多款全球软件/控件产品
针对软件研发的各个阶段提供专业培训与技术咨询
根据客户需求提供定制化的软件开发服务
全球知名设计软件,显著提升设计质量
打造以经营为中心,实现生产过程透明化管理
帮助企业合理产能分配,提高资源利用率
快速打造数字化生产线,实现全流程追溯
生产过程精准追溯,满足企业合规要求
以六西格玛为理论基础,实现产品质量全数字化管理
通过大屏电子看板,实现车间透明化管理
对设备进行全生命周期管理,提高设备综合利用率
实现设备数据的实时采集与监控
利用数字化技术提升油气勘探的效率和成功率
钻井计划优化、实时监控和风险评估
提供业务洞察与决策支持实现数据驱动决策
原创|行业资讯|编辑:郑恭琳|2015-08-31 10:49:24.000|阅读 547 次
概述:有关C#中15大被隐藏的顶级功能的第一个帖子是出现在Automate The Planet上。下面列出程序员在C#编程生涯中最喜欢的被隐藏的C#功能,包括完整的解释和代码示例。
#慧都22周年庆大促·界面/图表报表/文档/IDE/IOT/测试等千款热门软控件火热促销中>>
我不确定这些能不能被当作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@hmdbvip.cn




Sparx Systems Enterprise Architect作为基于UML的统一建模平台,通过全面的架构设计工具集,支持架构师从战略规划到技术落地的全过程,帮助企业在云地融合的复杂环境中构建可持续演进的技术基础。
HOOPS 3D可视化与建模集成方案为制造业研发团队提供了一套稳定、开放、可拓展的三维开发基础。
Parasoft为汽车电子系统中应用AI技术提出了一套路线图,帮助团队在技术创新与合规安全之间找到平衡。
创建易于访问且符合规范的 PDF 文档正成为各行各业日益重要的需求。在本篇bow中,我们将探讨如何使用 Text Control 的 .NET 库验证 PDF/UA 文档,轻松确保生成的 PDF 符合无障碍标准。
服务电话
重庆/ 023-68661681
华东/ 13452821722
华南/ 18100878085
华北/ 17347785263
客户支持
技术支持咨询服务
服务热线:400-700-1020
邮箱:sales@hmdbvip.cn
关注我们
地址 : 重庆市九龙坡区火炬大道69号6幢
永利最大(官方)网站