Home / Software / Tips and Tricks / What are the new Features added to c# 4.0

What are the new Features added to c# 4.0

Updated:  05/01/2010 03:05 AM
Author:  Shiju Mathews

Status:    Resolved.


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.

  • Named and Optional Parameters.

    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
    br>
    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:


    Overload Resolution

    Use of named and optional arguments affects overload resolution in the following ways:
  • A method, indexer, or constructor is a candidate for execution if each of its parameters either is optional or corresponds, by name or by position, to a single argument in the calling statement, and that argument can be converted to the type of the parameter.
  • If more than one candidate is found, overload resolution rules for preferred conversions are applied to the arguments that are explicitly specified. Omitted arguments for optional parameters are ignored.
  • If two candidates are judged to be equally good, preference goes to a candidate that does not have optional parameters for which arguments were omitted in the call. This is a consequence of a general preference in overload resolution for candidates that have fewer parameters.


  • Dynamic Support C# 4.0 supports Dynamic Programming by introducing new Dynamic Typed Objects. 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.

    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
  •