New Features of C # 4.0

New Features of C # 4.0


The new .NET Framework provides a better approach in terms of language support in C# Development with its version 4.0. With lots more features being added with the version, let us deal with new things in terms of method invocations and COM interoperability. These features enable us to create objects that can even conform to the standards of Dynamic Language Runtime. Some of the features which we would like to focus in this blog post are as follows:

1. Dynamic Lookup

2. Named and optional parameters

3. COM specific interop features

4. Variance

Dynamic Lookup

One of the core features introduced in C# 4.0 is called Dynamic Lookup . The function of the  Dynamic Lookup allows one to invoke things dynamically at run time. In Framework 3.5 and lower version,  we can create the method  and  invoke them.  When you call object methods, the compiler checks for  method  are called, and raises an error. This  compilation error can be over come with Dynamic Lookup. Using Dynamic Lookup we can call any method or property at runtime, and they are not checked until runtime. Not only method calls, but also field and property accesses, indexer and operator calls and even delegate invocations can be called dynamically. Look at the below scenario and know how each method, property, indexer and operator are called dynamically.

dynamic dyn= GetMyService();

dyn.Method(7); // calling methods

dyn.field = dyn.Property; // getting and settings fields and properties

dyn[“one”] = dyn[“two”]; // getting and setting thorugh indexers

int i = dyn + 3; // calling operators

string str = dyn(5,7); // invoking as a delegate

So at runtime a dynamic operation is called according to the nature of its target object d

Named and optional parameters

Optional arguments are widely used in VB for long time. Now this feature is included in the C # 4.0 too.

Optional arguments allows  to omit arguments when calling methods. This is possible by defining a method with assigning default values to parameters.

Define the method  mymethod with some default parameters.

public void mymethod(int a, int b = 50, int c = 100);

this method can be expressed in any one of the following way

mymethod(10, 30, 50);  as usual calling the method

mymethod(10, 30,); omitting the parameter c

mymethod(10,); omitting the parameters b and c

Named Arguments allows you to pass the arguments by the corresponding parameter names.

myMethod(a:10, b:30, c:50);

The above method can be rewritten as

myMethod(c:50, b:30, a:10);

myMethod(a:10, c:50); // This call Omits the parameter “b”.

COM specific interop features

C# 4.0 Development
provides much improved support for COM interoperability, making it far easier to call COM objects in side  .NET code. As discussed in the above section, both  dynamic lookup and  named and optional parameters which help improve the function of interoperating with COM APIs such as the Office Automation APIs.


One of the core features included in  C# 4.0 is called Variance. Variance is a property of operators that act on types or level. Variance can be understood in detail by going through the below scenario of

Covariance and Contra variance.

A variable of a higher level,  can hold an instance of small level called Covariance.


CEO ceo = new CEO();

HOD hod = new HOD();

HOD hod  = new CEO();

An instance smaller level  is substituted for a higher level. This is called Contra variance.

public void GetSalary(CEO ceo) { }

HOD hod = new HOD();


, , , , , ,