Friday, February 3, 2017

C# 6.0 Features

1. String interpolation by using symbol $

In older version of C# we used below syntax to format the string
var Output = string.Format("{0} is in class {2} ", 
    student.Name, student.Class);
C# 6.0 introduce a new way to format the string using $ sign

var Output = $"{student.Name} is in class {student.Class}th";
If student Name is Ahmad and class it 8th than Output will be

Ahmad is in class 8th.

If you want to print { than you can use double {{ like

var str =
    $" Square root of {{obj.Num}} is {Math.Sqrt(obj.Num)}";
If obj.Num=25 than Output will be

Square root of {25} is 5.

2. Auto Property Initializer


In older version of C# we intialize property like

public class Employee
{
    public string Name { get; set; } 
    public Employee() // constructor
    {
        Name = "Ahmad"; //assigment
    }
}

In new version you can initialize the property like

public class Employee
{
    public string Name
    {
       { get; set: } = "Ahmad";
    }
}


3.In C# 6.0 you can import a static class just like namespace and directly 
call its static method without prefix of class name.


Syntax
using static System.Console;

Example :-
using static System.Console;


namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {            
            Write("Write is a static method of Console class and it is called without classname")
        }        
    }
}

4. In C# 5.0 await was not permitted in catch and finally. But in C# 6.0 you can write await in catch and finally also.

        private static  async  void Method()
        {
            try
            {
                Console.WriteLine("New Thread");
            }
            catch (Exception)
            {
                Task.Run(new Action(Method2));
            }
            finally
            {
                Task.Run(new Action(Method3));
            }            
        }

5. nameof expressions

Ex
In older version of C# if you want to print the name of any enum member 
than you use it like

Console.Writeline(enmFileMode.Add.ToString());

But in version 6.0 you can use it like 


Console.Writeline(nameof(enmFileMode.Add);

Output will be same in both

"Add"

6. Dictionary Initializer

In older version of C# you can initialize a dictionary like

Dictionary cList = new Dictionary()
{
  {"A123", new Customer("A123")},
  {"B246", new Customer("B246")}
};

It is very complicated and ugly C# 6.0 gives a nice way to initialize a dictionary, Which is easier to read

Dictionary cList = new Dictionary()
{
  ["A123"] = new Customer("A123"),
  ["B246"] = new Customer("B246")
};
7. Exception Filter

In older version of C# you can filter your exception like

       private static void Method()
        {
            try
            {
                Console.WriteLine("New Thread");
            }
            catch (Exception ex) 
            {
                if(ex.Message == "RED")
                    throw ex;
            }
        }

In C# 6.0 you can do this like

        private static void Method()
        {
            try
            {
                Console.WriteLine("New Thread");
            }
            catch (Exception ex) when (ex.Message =="RED")
            {
                throw ex;
            }                    
        }


No comments:

Followers

Link