Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • What are New features in C# 6.0 ?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 430
    Comment on it

    Here are some features that are available in C# 6.0 -:

    1) Auto Property Initialization.

    2) Use of Conditional operator to check NULL values.

    3) nameof Expressions

    4) String Interpolation

    5) Exception filters

    6) Bodied Methods

    7) Using static

    Explanation

    1) Auto Property Initialization

    Previously to initialize a property we have to create constructor inside which we have to set default value for that property. Following is the example -:

    Older way

    Class Test
    {
      public int UserID { get; set; }
    
      public Test()
      {
        UserID = -1;
      }
    }

    Newer way

    Class Test
    {
      public int UserID { get; set; } = -1;
    }

    2) Use of Conditional operator to check NULL values

    We often need to apply NULL checks to avoid NULL reference exception.

    Older way

    int userId = user != null ? user.Id : -1;

    Newer way

    int userId = user?.Id ?? -1;

    3) nameof Expressions

    It will return string literal of name of properties/methods.

    Suppose we are logging name of method in catch block if any exception occur to improve logging. It help us to identify method in which exception occurs. Previously we write hard-coded name like  "Exception in GetUser()". But if we required to rename method say GetUserNew() we might forget to rename method in above hard-coded string. Now if exception comes exception log will contain "Exception in GetUser()" and we might search for GetUser() in our application but it will not found as we have renamed it. So by using nameof at time we rename function it will give error at compile-time to change the method name.

    Older way

    public static int MyFunction(int a, int b) 
    {
    
        try 
        {
           return a / b;
        }
        catch (Exception ex)
        {
           Console.WriteLine("Error in function MyFunction"); // We might forget to rename here.
           Console.ReadKey();
           return 0;
        }
    }

    Newer way

    public static int MyFunction(int a, int b) 
    {
    
        try 
        {
           return a / b;
        }
        catch (Exception ex)
        {
           Console.WriteLine("Error in function "+ nameof(Test.MyFunction)); // While renaming,    Will give compile time error here and we can rename.
           Console.ReadKey();
           return 0;
        }
    }

    4) String Interpolation

    Previously we use string.Format() method to write interpolated strings. In C# 6.0 we don't need Format() method for interpolation of strings.

    Older way

    string fName = "Michael";
    string lName = "Scofield";
    
    Console.WriteLine(string.Format("First Name -: {0} Last Name -: {1}", fName, lName));

    Newer way

    string fName = "Michael";
    string lName = "Scofield";
    
    Console.WriteLine($"First Name -: {fName} Last Name -: {lName}");

    5) Exception filters

    This feature gives power to apply conditions for catch blocks. Suppose I have to log ex.InnerException if its not null otherwise log ex.Message.

    Older way

    public static int MyFunction(int a, int b) 
    {
         try 
         {
             return a / b;
         }
         Catch(Exception ex)
         {
              if(ex.InnerException !=null)
              {
                 Console.WriteLine(ex.InnerException);
              }
              else
              {
                 Console.WriteLine(ex.Message);
              }
         }
    }
    

    Newer way

    public static int MyFunction(int a, int b) 
    {
         try 
         {
             return a / b;
         }
         Catch(Exception ex) when(ex.InnerException !=null)
         {
           Console.WriteLine(ex.InnerException);
         }
         Catch(Exception ex) 
         {
           Console.WriteLine(ex.Message);
         }
    }

    6) Bodied Methods

    Used for functions having single line return statement.

    Older way

    public static int GetSum(int a, int b)
    {
       return a+b;
    }

    Newer way

    public static int GetSum(int a, int b) => a+b;

    7) Using static

    Previously for calling static methods or properties we need to write something like ClassName.MethodName() / ClassName.PropertyName. With new feature we don't need to invoke using ClassName but we have include using static System.ClassName (ex-: Convert) namespace. For following example include "using static System.Convert". You can also include "using static System.Console" to driectly call WriteLine() method of class Console.

    Older way

    Console.WriteLine("Enter any two numbers");
    int a = Convert.ToInt32(Console.ReadLine());
    int b = Convert.ToInt32(Console.ReadLine());
    Console.WriteLine(Test.GetSum(a, b));

    Newer way

    Console.WriteLine("Enter any two numbers");
    int a = ToInt32(Console.ReadLine());
    int b = ToInt32(Console.ReadLine());
    Console.WriteLine(Test.GetSum(a, b));

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: