Home > Articles > C# Articles > New features in C# 6.0

New features in C# 6.0

by Thamil   on Sep 26, 2015   Category: C#  | Level: Intermediate  |  Views: 1110    |  Points: 100   
Like this article? Bookmark and Share:
C# 6.0 released with .net framework 4.6 and visual studio 2015. In this article we will discuss about few important features which are available in C#6.0 and every developer must know about these features and improve the coding and readability.

1. Auto property initializers

   

Prior to C# 6.0, we need to set the property value through constructor in a class, to simplify this C# 6.0 Auto-property initializer enables to set the value of a property without constructor. See the below sample Here few think needs to be considered, to initialize the property we must explicitly declare a constructor.

    public class Employee
    {
        public string Name{ get; set; }
        public Employee()
        {
            Name = "Mike";
        }
     }
    

To simplify this, without explicit constructor we can achieve the above by using auto property initializers as like below.

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

Declare an auto property as like above and use assignment operator to assign the value and make it readable and fast coding.

Getter or read only auto properties:

We can able to create the auto propertied without setters as like below and getter only auto property is implicitly declared as read only.

    public class Employee
    {
        public string FirstNmae { get; } = "Mike";
    }
    

2.Using Static

   

Using static is a new feature in C# which imports the static members of type into the scope and this feature would reduce the duplication

using static System.Console;
public class Employee
{
    public static void Main()
    {
        WriteLine("Hello employee!");
    }
}

Above System.Console static c lass is imported in namespace area , so that we can directly use WriteLine .

3.Null conditional operators

   

To prevent Nullreferenceexception we used to perform the null checks before invoking member or objects or properties. Prior c#6.0 we used to check the null in the below way

    private void GetEmpSalary(Employee emp)
    {
        double empsalary = 0;

        if (emp != null && emp.Details != null)
        {
            empsalary = emp.Details.Salary;
        }
    }

Same above null check can be converted into one line by using null conditional operator as shown below in C# 6.0. The null conditional operator allows us to access members and elements only when receiver is not null.

    private void GetEmpSalary(Employee emp)
    {
        double empsalary = emp?.Details?.Salary ?? 0;
    }
    

Also we can use null conditional operator in the below way as well.

    private void otherway()
    { 
        int? length = room?.Length; // it would return null if room is null
        int length = room?.Length??0; // it would return 0 if room is null
    }
    

 

4.Index initializers

   

Index initializers provide a ways to initialize the index based collection such as dictionaries and objects as well.

Prior c#

    Dictionary  GameList = new Dictionary
    {
    {1,  "Cricket" },
                {2,  "Football"}, 
          {3, "Tennis" }
    }
    

In C# 6.0, removed the curly brackets because we have already curly brackets in beginning and end of initialization so it’s more readable

    Dictionary  GameList = new Dictionary
    {
    [1] = "Cricket", 
                [2] = "Football", 
            [3] = "Tennis" 
    }
    

 

5.String interpolation

   

String interpolation is used to format the string and it replaces the String. Format, we can directly apply the string expressions or variables instead of the place holders. See the below sample.

Before C#6.0

     string Name = "Mike";
     string Game = "Cricket";
     var format = String.Format("{0}'s favourite game is {1}", Name, Game);
    

In C#6.0

    string Name = "Mike";
    string Game = "Cricket";
    var format = $"{Name}'s favourite game is {Game}";
    

It’s a template string that contains the expression and it’s easier to understand

6.Expression bodied function member

   

Expression bodied members used to create the expression instead of statement blocks and without any curly braces or any explicit returns. So that we can avoid writing a method for a single line of code. See the below sample code.

    public string GetEMPName => $"{FirstName} {LastName}";
    

Thanks for reading and hope you enjoyed this article.



Like this article? Bookmark and Share:

Most viewed Articles

User Comments


No response found, be the first to review this article.

Submit feedback about this article

Please sign in to post feedback

Latest Posts