What's New with C# 4.0??

If you have worked with C# vastly, you know it very well that it is a static typed language which means that before you can compile the code you have to have Type defined for variable( at least in most of scenarios). Contrary to this in real situation we see that ,inherent nature of objects is ,being Dynamic. This point is the main theme of C# 4.0. There are many very good improvements in C# 4.0 in terms of features/interoperability with COM etc. In this post i will just List out the feature level improvements and small details of them. (with a promise that i will have all of them explained with working code in upcoming posts)

1- Dynamic Look Up: Dynamic lookup allows you to write method, operator and indexer calls, property and field accesses, and even object invocations which bypass the C# static type checking and instead gets resolved at runtime.

C# introduces a new static type “dynamic” , using which you can perform the operation which gets resolved only at run-time. C# compiler allows you to call any method with any name and any signature with such dynamic types. Only during run time , the object will be examined to see what that call actually means.

2- Named and Optional parameters: in C# , we tend to create overload of a method  to support calling with distinct number of parameters. there are situation where we are forced to pass parameters ,even though those might not be actually used in that call(or we will need those many overloads of that method). the problem gets worse when we use office PIA’s where there are number of parameters, which we know are optional. But we still have to create dummy parameters and pass it to method calls. C# 4.0 simplifies this approach with the introduction of “Optional Parameters”. So during the method declaration, we can now specify the default value and this value will be used , in case not value is passed by caller.

ex.  public bool GetCountOfMethods(int i , string a=”test”);

We can call this method in two ways


and GetCountOfMethods(5 , “Don’t test”);

In case of first call ,  the default value i.e.”test” will be used for argument a.

The other important feature if Named Arguments. So now it is not compulsory to pass the arguments in the order they are in method declaration. Instead you can pass the arguments by parameters names. for ex. above method can be called like

GetCountOfMethods(i:6 , a:”Foo”)  

and Order of parameters also doesn’t matter if names are provided for method arguments.

3-COM specific improvements: The above two feature helps a lot when working with COM API’s in C# 4.0 . but there are couple of COM specific improvements also which simplifies the work of developer even furthur.

a- Dynamic Import: Many COM methods accept and returns variants types and are represented in PIA’s as object types. So even though a developer knows the static type of a returned object , he is required to perform a explicit cast operation on the returned value to make use of it.

To facilitate the better development experience , it is not possible to use dynamic types instead of variants in COM PIA’s. and then a static type value can be directly assigned to it , with explicit cast operation.

This means that you can easily access members directly off a returned object, or you can assign it to a strongly typed local variable without having to cast. To illustrate, you can now say

excel.Cells[1, 1].Value = "Hello";

instead of

((Excel.Range)excel.Cells[1, 1]).Value2 = "Hello";


Excel.Range range = excel.Cells[1, 1];

instead of

Excel.Range range = (Excel.Range)excel.Cells[1, 1];

b. Omitting ref : Due to difference in programming model , COM PIA’s has lot of reference parameters (which is entirely different from C# passing by reference). There for in C# 4.0  , all such parameters can now be passed by value . C# compiler will take care of  generating temporary variables to hold the passed-in values and discarding the returned values. It simplifies the programming syntax greatly.


Technorati Tags: C# 4.0,C#,dynamic in C#,VS 2010