C# Features From 3.0 to 6.0

C# 3.0 features

  1. Var keyword:

    Implicitly typed local variable. Need to initialize at the time of declaration. The data type of a ‘var’ variable will be determined when assigning values to variables. After assigning the variable value, the variable has a defined data type and cannot be replaced.

    E.g.
    1. var abc=1;  
  2. Auto Properties:

    Auto implemented properties.

    E.g.
    1. Public stirng Name {getset;}  
  3. Anonymous Types:

    An anonymous type is a simple class created on the fly to store a set of values. To create an anonymous type, you use the new keyword followed by an object initializer, specifying the properties and values the type will contain.

    E.g.
    1. var EmpName= new {Name=”FirstName”, Age=20};  
  4. Extension Methods:

    Extension methods allow an existing type to be extended with new methods, without altering the definition of the original type. An extension method is a static method of a static class, where the “this” modifier is applied to the first parameter. The type of the first parameter will be the type that is extended.

    E.g.
    1. public static class ABC  
    2. {  
    3.    public static int ChangeData(this string str)  
    4.    {  
    5.       return int.Parse(str);  
    6.    }  
    7. }  
  5. Lambda Expressions:

    A lambda expression is an unnamed method written in place of a delegate instance.

    A lambda expression has the following form:

    (parameters) => expression-or-statement-block

    E.g.
    1. X=> x*x;  

C# 4.0 features

  1. Named and Optional Parameters:

    Optional Parameters:

    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. This comes in handy when you have overloaded methods that are chained together.

    E.g.
    1. public void Process( string data, bool IsAllowed = false, ArrayList data = null )  
    2. {  
    3.   
    4. }  
    The following statements are valid:
    1. Process(“Rakesh”);  
    2. Process(“Rakesh”, true);  
    3. Process(“Rakesh”,true,list); (where list is the object of List)  
    Named Parameters:
    1. Process(“Rakesh”,list);   
    Above statement is invalid as we have omitted 2nd bool parameter. But we can use the following method using named parameters feature.
    1. Process(“Rakesh”, data:list);  
  2. Dynamic:

    Dynamic are dynamically typed variables. No need to initialize the dynamic variables at time of declaration. Dynamic variables can be used to create properties and return values from a function. The following syntax is valid using dynamic.
    1. dynamic data=100;  
    2. data=”Welcome to C#”;  
  3. Co-Variance and Contra-Variance:

    Co-Variance:

    Co- variance preserves assignment compatibility between parent and child during dynamic polymorphism.

    E.g.

    The following syntax is possible in C# 4.0:
    1. Public class Animal  
    2. {}  
    3.   
    4. Public class Cat: Animal  
    5. {}  
    6.   
    7. Public class Program  
    8. {  
    9.     Public static void main()  
    10.     {  
    11.         IEnumerable < Animal > animals = new List < Cat > (); // not valid in earlier C# versions   
    12.     }  
    13. }  

C# 5.0 features

  1. Async (Asynchronous methods):

    C# 5.0 Async feature introduces two keywords async and await which allows you to write asynchronous code more easily. Async and await are the code markers which mark code positions from where control should resume after a thread (task) completes.
    Async and await must be used in a pair.

    E.g.
    1. Public static Async void Method()  
    2. {  
    3.    await Task.Run(new Action(LongTask));  
    4. }  
  2. Caller Information:

    Caller information can help us in tracing, debugging and creating diagnose tools. It will help us to avoid duplicate codes which are generally invoked in many methods for same purpose, such as logging and tracing.

    • CallerFilePathAttribute
    • CallerLineNumberAttribute
    • CallerMemberNameAttribute

C# 6.0 features

  1. Using Static:

    This is the new concept of C# 6.0. No need to write Console.Writeline(“Ritesh”) or Console.Write().

    Just add using statement at top and use static methods directly.
    1. Using System.Console; 
    2.  
    3. class Program
    4. {
    5. static void Main(string[] args)
    6. {
    7.    WriteLine(“Ritesh”);  
    8.    ReadLine();  
    9. }
    10. }
  2. Auto Property Initializer:

    This is a concept to set the value of a property during property declaration. We can set the default value of a readonly property, it means a property that only has a {get;} attribute.

    E.g.
    1. Public Class Employee  
    2. {  
    3.    Public string Name {get;set;}=”Suresh”;  
    4.    Public int Age {get;set;}=25;  
    5. }  
  3. Dictionary Initializer:

    We can directly initialize a value of a key in a Dictionary Collection with it, either the key in the collection would be a string data type or any other data type.

    E.g.
    1. Dictionary < intstring > dicInt = new Dictionary < intstring > ()  
    2. {  
    3.     [1] = ”Rakesh”, [2] = ”Amar”, [3] = ”Nilesh”  
    4. };  
  4. Exception Filters:

    Exception filters allows us to specify a condition in Catch block. So if the condition will return true then the catch block is executed only if the condition is satisfied.

    E.g.
    1. Catch(Exception ex) if(val==0)  
    2. {  
    3.    Console.writeline(“Divide by zero exception”);  
    4. }  
  5. Easily format strings using String interpolation:

    To easily format a string value in C# 6.0 without any string.Format() method we can write a format for a string. It's a very useful and time consuming process to define multiple string values by “\{ variable }”.

    E.g.
    1. String Output= “\{FirstName} - \ {LastName}”;  
  6. Paramterless Constructors in Struct

    We can create a parameterless constructor in a struct explicitly in Visual Studio 2015.
    1. Public struct Student  
    2. {  
    3.     int rollno;  
    4.     string Name;  
    5.   
    6.     public Student()  
    7.     {  
    8.         Rollno = 0;  
    9.         Name = string.empty;  
    10.     }  
    11. }  

References:


Similar Articles