Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

c# 4.0 new features

0.00/5 (No votes)
7 Jun 2011 1  
new features of c# 4.0

Introduction

C# 4.0 includes following some great features.

Dynamic Types

C# 4.0 supports dynamic types. This functionality is most useful in dynamic languages that require runtime bindings. Using Dynamic types you can defer the type checking to Runtime

csharp-4-dynamic.JPG

Optional Parameter

Using Optional Parameter, We can avoid creating many overloads of methods by specifying default values of some parameters.

csharp-4-optional-parameter.JPG

Named Parameter

In C# 4.0, We can call the methods with named parameter

csharp-4-named-parameter.JPG

Simplify COM interop

In C# 4.0, We can simplify the COM interop operations using dynamic type and optional parameters..

csharp-4-com-interop.JPG

Variance

Variance is the collective term for covariance and contravariance, two concepts that have been introduced in .NET 4.


From wikipedia:-
covariant - converting from wider (double) to narrower (float).
contravariant - converting from narrower (float) to wider (double)
invariant-Not able to convert

Manager _manager = New Manager();
Employee _employee = _manager;

Here, an object of type Manager has been placed into a variable of type Employee — which is possible because Manager derives from Employee.


However, the same cannot be possible in generics (C# 3.0)
IEnumerable<Manager> _managerList = New IEnumerable<Manager>();
IEnumerable<Employee> _employeeList = _managerList;

In C# 4.0, It is possible because Generics supports Variance.
csharp-4-variance.JPG

Conclusion

This tutorial was created to help you understand the new features of C# 4.0. I Hope you have found this tutorial helpful and interesting. This article is same as my original post C# 4.0 new features

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here