Get Ready For C# 4.0!
来源:互联网 发布:linux系统管理员书籍 编辑:程序博客网 时间:2024/05/18 03:03
Visual Studio 2010 is here! And of course this means that C# 4.0 is also here. Let’s do a quick review of the new language features added in this release.
Dynamic
The dynamic keyword is a key feature of this release. It closes the gap between dynamic and statically-typed languages. Now you can create dynamic objects and let their types be determined at run time. With the addition of the System.Dynamic namespace, you can create expandable objects and advanced class wrappers, and you can provide interoperability between different languages, including dynamic ones. Here is one quick example:
contact.Name = "Patrick Hines";
contact.Phone = "206-555-0144";
I have discussed the pros and cons of this feature on this blog. If you want to know more, read here.
Covariance and Contravariance
Variance on generic type parameters in interfaces and delegates is another important feature of this release. It doesn’t add much new functionality, but rather makes things work as you expected them to in the first place. The major advantage is hidden in this simple line, which didn’t compile until C# 4.0:
The ability to implicitly convert references for objects instantiated with different type arguments makes it much easier to reuse code. Read the Covariance and Contravariance FAQ to learn more about this feature.
Optional (or Default) Parameters
Looking at the archives of this blog, I see that people have been asking for this feature since C# 1.0. Three versions later, it’s finally here.
Now you can assign a default value to a parameter right within the method declaration. The user of the method can either pass a value or simply skip the argument. In the latter case, the default value is passed to the method.
Method declaration:
Method calls:
SomeMethod(10);
Named Arguments
The order of parameters in a method declaration and the order of arguments you pass when calling the method don’t need to match anymore. You can provide arguments in any order you like by specifying parameter names in a method call. This might also improve the readability of your code.
sample.InsertRange(collection: new List<String>(), index: 0);
sample.InsertRange(index: 0, collection: new List<String>());
Read more about optional parameters and named arguments on MSDN.
Improved COM Interop
The introduction of the dynamic keyword, optional parameters and named arguments enables improvement of COM interop. So, no more ugly code like this:
// . . .
excelApp.get_Range("A1", "B4").AutoFormat(
Excel.XlRangeAutoFormat.xlRangeAutoFormatTable3,
Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing);
You can now simply write the following:
Excel.XlRangeAutoFormat.xlRangeAutoFormatClassic2);
By the way, this code uses one more new feature: indexed properties (take a closer look at those square brackets after Range.) But this feature is available only for COM interop; you cannot create your own indexed properties in C# 4.0.
For more information about new COM interop features, once again, refer to MSDN.
What Else?
Of course, C# benefits not only from new language features, but also from improvements to its integrated development environment (IDE) and to the .NET Framework.
Here are some links for further reading:
- Find all calls to and from your methods with the new Call Hierarchy window.
- Even if you have never heard of test-driven development (TDD), the Generate From Usage feature may help you to quickly prototype your application by generating method and class stubs, allowing you to concentrate on your current task rather than worrying about declarations. For TDD adepts, this feature might be a real performance booster. Check out this walkthrough for more details.
- Don’t forget to take a look at What's New in the .NET Framework 4 and also at What's New in Visual C# 2010 (just to make sure I didn’t miss something).
- And just in case, check out breaking changes for C# and .NET Framework.
- Last, but not least, don’t forget to report any problems at Microsoft Connect.
- Update. See my new post: New IDE Features in Visual Studio 2010 for C# Developers.
Happy coding, and thanks to all of you who helped to make this release better by providing feedback!
- Get Ready For C# 4.0!
- get ready for Android
- Get Ready for SaaS 2.0
- Get Cordova Ready for Grunt and CoffeeScript
- Ready for Notebook
- ready for wedding
- ready method for jQuery
- ready for preview
- gRPC Wait for Ready
- Getting ready for the cloud
- Ready for Beijing. Go! Go!
- Is Docker ready for production?
- Sicily 1738 get ready? (基础题)
- Is VS.NET ready for enterprise? (1)
- Is VS.NET ready for enterprise? (2)
- Is VS.NET ready for enterprise? (3)
- Is VS.NET ready for enterprise? (4)
- Is VS.NET ready for enterprise? (5)
- 玲珑酒器 艳美酒色
- 二叉树创建及遍历算法(递归及非递归)
- Android开发学习网站汇总
- android windows的环境配置
- WINCE的原子操作
- Get Ready For C# 4.0!
- .NET弹出提示点击确定之后再跳转页面的方法
- 高内聚低耦合
- PHP 获取文件扩展名的正确方法
- Struts hibernate 集成问题
- java socket 通信
- js 操作 框架对象 以及 框架对象中的方法或者属性
- VC运行库详解
- 2.10如何使文档自动保存而不用提示用户?