C# 4.0 的新酷特性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/292265/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
New Cool Features of C# 4.0
提问by bendewey
What are the coolest new features that you guys are looking for, or that you've heard are releasing in c# 4.0.
你们正在寻找或听说在 c# 4.0 中发布的最酷的新功能是什么?
回答by Ben Scheirman
the dynamic keyword looks like it can bridge the gap between dynamic languages like IronRuby or IronPython quite nicely, which will probably help its adoption in the Microsoft monoculture... that excites me.
dynamic 关键字看起来可以很好地弥合 IronRuby 或 IronPython 等动态语言之间的差距,这可能有助于它在 Microsoft 单一文化中的采用......这让我很兴奋。
While I'm intrigued by it, I'm also worried that it will be overused, like Generics and LINQ, SQLCLR, etc :)
虽然我对它很感兴趣,但我也担心它会被过度使用,例如泛型和 LINQ、SQLCLR 等 :)
回答by Jon Skeet
The dynamic stuff sounds cool if you need itbut I don't expect to use it very often.
如果您需要,动态的东西听起来很酷,但我不希望经常使用它。
The generic variance for delegates and interfaces is similar - the lack of variance is a headache at the moment, but many of the places where it's a pain won't be covered by the limited variance available in C# 4.
委托和接口的通用差异是相似的 - 缺乏差异是目前令人头疼的问题,但是 C# 4 中可用的有限差异不会涵盖许多令人痛苦的地方。
The COM features don't particularly interest me - I really ought to get more of a handle on what they are though.
COM 功能对我不是特别感兴趣——我真的应该更多地了解它们是什么。
Optional and named parameters could make a big difference when building immutable types: it enables syntax like:
在构建不可变类型时,可选参数和命名参数可能会产生很大的不同:它启用如下语法:
Person p = new Person (name: "Jon", age: 32);
without having mammoth combinations of constructor overloads. I'd prefer a bit more support for writing immutable types in the form of readonly automatically implemented properties, but I don't expect we'll get those. (They certainly aren't in the proposed feature list at the moment.)
没有构造函数重载的庞大组合。我更喜欢以只读自动实现的属性的形式编写不可变类型的更多支持,但我不希望我们会得到这些。(它们目前当然不在提议的功能列表中。)
I'm personally actually more interested in a couple of the frameworkfeatures of .NET 4.0 - in particular code contractsand parallel extensions.
回答by CMS
Method parameter default values:
方法参数默认值:
public void MyMethod1(string value1 = "test", int num1 = 10, double num2 = 12.2)
{
//...
}
Also maybe anonymous return types:
也可能是匿名返回类型:
public var MyMethod2()
{
// ..
}
回答by Bryan Watts
IDynamicObject
, the glue behind dynamic
, allows interpretation of a call at runtime.
IDynamicObject
后面的胶水dynamic
允许在运行时解释调用。
This is interesting for inherently untyped scenarios such as REST, XML, COM, DataSet
, dynamic languages, and many others. It is an implementation of dynamic dispatchbuilt on top of the Dynamic Language Runtime (DLR).
这对于固有的无类型场景很有趣,例如 REST、XML、COM DataSet
、动态语言等。它是建立在动态语言运行时 (DLR)之上的动态调度的实现。
Instead of cumbersome reflection semantics, you dot intovariables declared as dynamic
. Imagine working with Javascript objects from Silverlight:
代替繁琐的反射语义,您可以插入声明为dynamic
. 想象一下使用 Silverlight 中的 Javascript 对象:
dynamic obj = GetScriptObject();
HtmlPage.Window.Alert(obj.someProperty);
All C# syntax is supported (I believe):
支持所有 C# 语法(我相信):
HtmlPage.Window.Alert(obj.someMethod() + obj.items[0]);
Reflection itself looks a lot cleaner:
反射本身看起来更干净:
public void WriteSomePropertyValue(object target)
{
Console.WriteLine((target as dynamic).SomeProperty);
}
public void WriteSomeMethodValue(object target, int arg1, string arg2)
{
Console.WriteLine((target as dynamic).SomeMethod(arg1, arg2));
}
dynamic
is another tool in the toolkit. It does not change the static vs. dynamic debate, it simply eases the friction.
dynamic
是工具包中的另一个工具。它不会改变静态与动态的争论,它只是缓解了摩擦。
回答by Chris Shouts
Enhanced support for Expression Trees!
增强了对表达式树的支持!
回答by si618
Not strictly C#, but since .NET is tagged here's a good link regarding BCL changes.
不是严格的 C#,但由于 .NET 被标记为这里是关于BCL 更改的一个很好的链接。
Note to self: Must rename my Stopwatch.Restart()
extension method before 4.0 is released :)
自我注意:必须Stopwatch.Restart()
在 4.0 发布之前重命名我的扩展方法:)
回答by Amir Rezaei
- C# 2.0—Generics (.NET Framework support was added, and C# benefited from this); iterator pattern (the yield keyword); anonymous methods (the delegate keyword), nullable types, and the null coalescing operator (??).
- C# 3.0—Anonymous types, extension methods, object initializers, collection initializers, implicitly typed local variables (var keyword), lambda expressions (=>), and the LINQ query expression pattern.
- C# 4.0—Optional Parameters and Named Arguments, Dynamic typing (dynamic type), improved COM-Interop, and Contra and Co-Variance.
- C# 2.0 —泛型(添加了.NET Framework 支持,C# 从中受益);迭代器模式(yield 关键字);匿名方法(delegate 关键字)、可为空类型和空值合并运算符 (??)。
- C# 3.0— 匿名类型、扩展方法、对象初始值设定项、集合初始值设定项、隐式类型局部变量(var 关键字)、lambda 表达式 (=>) 和 LINQ 查询表达式模式。
- C# 4.0 —可选参数和命名参数、动态类型(动态类型)、改进的 COM-Interop 以及 Contra 和 Co-Variance。
回答by Amir Rezaei
C#4.0
C#4.0
1) PLinq
1)PLinq
2) Named and Optional Parameters
2) 命名和可选参数
3) Lazy
3) 懒惰
4) Co & Contra Variance
4) 协方差
5) Task Parallel
5) 任务并行
6) Dynamic object
6) 动态对象
7) Expando object
7) 扩展对象
8) Improved COM-Interop
8) 改进的 COM-Interop
9)Tuple
9)元组
only to name a few
仅举几例
回答by Embedd_Khurja
Ability to write asynchronous code in synchronous fashion with asyncand awaitis cool.
使用async和await以同步方式编写异步代码的能力 很酷。