Monday 3 May 2010

Features new to C# 4

Here’s a smattering of what’s new in C# 4.

Dynamic support

There is a new keyword – dynamic. This keyword allows you to create dynamic objects and let their types (and therefore operations) be determined at run time. This feature facilitates interoperability with other languages including dynamic languages and COM Interop.

The dynamic keyword is easy to use:

dynamic myObject = new SomeObject();
myObject.MyProperty = "SomeValue";
myObject.DoSomething();

Note that with dynamic objects the compiler won’t catch typos in member names etc. Everything is evaluated at runtime.

“The dynamic type enables the operations in which it occurs to bypass compile-time type checking. Instead, these operations are resolved at run time. The type simplifies access to COM APIs such as the Office Automation APIs, and also to dynamic APIs such as IronPython libraries, and to the HTML Document Object Model (DOM).” - http://msdn.microsoft.com/en-us/library/dd264736%28VS.100%29.aspx

There is also a new ExpandoObject class (part of the DLR):

“The ExpandoObject class enables you to add and delete members of its instances at run time and also to set and get values of these members. This class supports dynamic binding, which enables you to use standard syntax like sampleObject.sampleMember instead of more complex syntax like sampleObject.GetAttribute("sampleMember").” - http://msdn.microsoft.com/en-us/library/system.dynamic.expandoobject%28VS.100%29.aspx

You can just add new members on the fly to ExpandoObject instances:

dynamic sampleObject = new ExpandoObject();
sampleObject.test = “Dynamic Property”;

Optional/default parameters

This language feature enables you to specify a parameter value to be used if the parameter is omitted from a call.

Example method declaration:

public static void SomeMethod(int optional = 0) { }

Example method calls:

SomeMethod(); // 0 is used in the method.
SomeMethod(10);

This might be useful with MVC action methods where previously nullable parameters were used for optional URL parameters.

Named arguments

This language feature could be useful in conjunction with optional parameters.

“Named arguments enable you to specify an argument for a particular parameter by associating the argument with the parameter's name rather than with the parameter's position in the parameter list. Optional arguments enable you to omit arguments for some parameters. Both techniques can be used with methods, indexers, constructors, and delegates.” - (my italics) http://msdn.microsoft.com/en-us/library/dd264739%28VS.100%29.aspx

Example method declaration:

public void ExampleMethod(int required, string optionalstr = "default string", int optionalint = 10)

Example method calls:

anExample.ExampleMethod(3); // one required parameter, 2 optional

anExample.ExampleMethod(3, optionalint: 4); anExample.ExampleMethod(3, optionalstr: “test”); anExample.ExampleMethod(3, optionalint: 4, optionalstr: “test”);

Covariance and contravariance

Covariance enables you to use a more derived type than that specified by the generic parameter, whereas contravariance enables you to use a less derived type.

See http://msdn.microsoft.com/en-us/library/ee207183.aspx.

See also http://msdn.microsoft.com/en-us/library/dd469484.aspx for the in keyword and http://msdn.microsoft.com/en-us/library/dd469487.aspx for the out keyword.

Explained nicely in this web cast: http://channel9.msdn.com/posts/bruceky/Whirlwind-13-Whats-new-in-C-4-Covariance--Contravariance/.