view more software Tips and Tricks
Updated: 05/01/2010 03:05 AM
Author: Shiju Mathews
Let us take the new features in look at c# 4.0. The following are the new features added in the new version.
Microsoft breaks the new features into the following four categories.
Support for optional parameters allows you to give a method parameter a default value so that you do not have to specify it every time you call the method.
Let us consider a function CalculateInterest
In older versions of c# we can only call this method by only by using the exact same signatre of the methos with three argumnets that is exactly the same oreder and data type.
But in c# 4.0 we can use optional parameters as follows.
c# 4.0 allow us to call the functions as
We can also use named parameters as follows:
Use of named and optional arguments affects overload resolution in the following ways:
The type of these objects is resolved at run-time instead of at compile-time. A new keyword dynamic is introduced to declare dynamic typed object. The keyword tells the compiler that everything to do with the object, declared as dynamic, should be done dynamically at the run-time using Dynamic Language Runtime(DLR). Consider the following class.
Programmers will typecast the object that is receiving from a to match with the declared type.
Typecast is required because “GetAProgrammer()” returns a object that is not matching with the “programmer” object (Remember in object oriented language a child class object cannot hold the parent class object. ).
But in c# 4.0, using the dynamic keyword you can write
The dynamic keyword is new to C# 4.0, and is used to tell the compiler that a variable's type can change or that it is not known until runtime.
dynamic will work with properties too.
There many times variable reuse in the code as flows.
In c# 3.5
In c# 4.0 using dynamic
There are other features like ‘Covariance and Contravariance’ added to C# 4.0