The C# team posts answers to common questions and describes new language features
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.
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:
I have discussed the pros and cons of this feature on this blog. If you want to know more, read here.
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.
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.
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.
Read more about optional parameters and named arguments on MSDN.
The introduction of the dynamic keyword, optional parameters and named arguments enables improvement of COM interop. So, no more ugly code like this:
You can now simply write the following:
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.
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:
Happy coding, and thanks to all of you who helped to make this release better by providing feedback!
I enjoy C# programming, I wonder what else may be added to new versions. i think it is already a full answered for every project, but i bleive in future.
i hope dynamic will not break the existing code.
when i have a class with name dynamic, which works well with .net 2.0
We are in a planning stage still, so it's hard to tell what the plans for the next version are. Again, I'd recommend to go to Microsoft Connect. Probably, this is something similar to what you are talking about: http://connect.microsoft.com/VisualStudio/feedback/details/94264/arithmetic-types-like-int-double-decimal-should-implement-iarithmetic-t
Vote for the issue and leave your comments, so the team can answer you. There is a really long discussion there already.
"dynamic" is a contextual keyword. It means that it's not reserved, so you can use it as a class or class member name.
Yes, Default Paramater... FINALLY
Really Cool... C# 4.0 will rock. I really like the dynamic keyword feature of this language as well as COM interop.
I wonder why it took sooooo long to add optional and named parameters in C#? Why was not included in the C# 1.0?
Yes. Finally, optional parameter.
Dynamic features is very interesting and very good...
Can you start form zero? Because me is newbie,,,
dynamic contact = new ExpandoObject();
contact.Name = "Patrick Hines";
contact.Phone = "206-555-0144";