Click here to Skip to main content
15,885,985 members
Articles / Programming Languages / C#

C# 6.0 New Features

Rate me:
Please Sign up or sign in to vote.
3.92/5 (21 votes)
24 Feb 2015CPOL2 min read 25.6K   18   12
New features of C# 6.0

C# 6 is available with Visual Studio 2015 and it comes with some new features. To try C# 6, download and install the Visual Studio 2015 preview.

Here are some of the new features that are available in C# 6.

1) Auto Property Initializers

You can now assign the value to a property directly at the declaration place. With read only properties (getter only), you can assign the value at the declaration or in the constructor.

Before

C#
public class Employee
{
    public Guid EmpId {get;set;}

    public Employee()
    {
        EmpId = Guid.NewGuid();
    }
}

C# 6

C#
public class Employee
{
    public Guid EmpId {get;set;} = Guid.NewGuid();

    //Works with read only property also
    public string FullName{get;} = "Madhur Kapoor";
}

Using this type of initialization, the setter function is not invoked internally, the backing field is directly assigned the value.

2) Expression-bodied Members

If you have got a property/method in your code that contains a single line of code, you can use the “=>” operator to express it instead of defining the body using curly braces.

Before

C#
public void PrineName()
{
    Console.Writeline(emp.FullName);
}

public string FullName { get {return emp.FirstName + em.LastName; } }

C# 6

C#
public void PrineName() => Console.Writeline(emp.FullName);
public string FullName => emp.FirstName + emp.LastName;

This can only be used with single line functions. Though it does not offer anything useful, it does make the code look a bit readable.

3) Using “Static” Class Import

You can specify a particular type in the using statement and all the static members in that type will be available in the code:

C#
using System.Console;

  class Program
    {
        static void Main(string[] args)
        {
            //Using WriteLine directly instead of 
            // Console.WriteLine
            WriteLine("Hellow World");
        }
    }

4) Exception Filters

Exception Filters can be used to specify a condition for the catch block. The catch block will only be executed if the condition is true.

C#
try
{
    throw new MyException { Severity = 3 };
}
catch (MyException ex) if (ex.Severity == 2)
{
    Console.WriteLine("Will not execute");
}
catch (MyException ex) if (ex.Severity == 3)
{
    Console.WriteLine("Will be executed");
}

5) String Interpolation

Before

C#
string fullname = "Madhur Kapoor"
Console.Writeline("Name - " + fullname);

C# 6

C#
string fullname = "Madhur Kapoor"
Console.Writeline("Name - \{fullname}" );

6) Dictionary Initializer

The syntax for initializing dictionaries is now more readable and clear. It makes the code more easier to read.

Before

C#
Dictionary<string, string> eplTeams = new Dictionary<string, string>()
{
    { "Arsenal", "ARS" },
    { "Burnley", "BUR" },
    { "Manchester United", "MUN" }
};

C# 6

C#
Dictionary<string, string> eplTeams = new Dictionary<string, string>()
{
    ["Arsenal]  =  "ARS",
    ["Burnley"] =  "BUR",
    ["Manchester United"] = "MUN"
};

7) Await in Catch block

In the earlier versions of C#, using ‘await’ in catch and finally blocks was not available. This can be quite helpful if you want to log some exception to file/database and you don’t want to block the main thread.

C#
try
{
    DoWork()
}
catch (Exception ex)
{
    await Log.WriteDatabase(ex);
}

8) Null Conditional Operator

As programmers, we do a lot of null condition checks in our code. With the new null condition operator, you can do a lot of null check in a single line of code using the “?” & “??” operators.

Before

C#
if(employee != null && employee.ContactDetails != null)
{
    Console.WriteLine(employee.Name + "-" + employee.ContactDetails.Address);
}

C# 6

C#
Console.WriteLine(employee?.Name + "-" + employee?.ContactDetails?.Address?? " No Details");

In “employee?.Name”, if the object is not null, then Name will be printed. The “??” operator can be used to print some other information if the object is null. This feature saves a lot of lines of code which were earlier used for null checks.

As it is still in preview mode, some features might change when the final version comes out.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead Infosys
India India
Madhur is Technology Lead by profession having around 9+ yrs of experience in IT industry working on Microsoft Technologies. Apart from Microsoft Technologies, he also likes to work on Mobile Development in Android.

His Technical expertise include .Net technologies including MVC, WebAPI, Azure.

Apart from coding, he like to play Xbox Games, strum guitar or read books.

My Blog : http://www.codingparadox.com

Comments and Discussions

 
SuggestionUse of Static Members with namespace Pin
arunporwal4-Nov-15 7:06
arunporwal4-Nov-15 7:06 
SuggestionWe can't use System.Console without static keywork Pin
arunporwal4-Nov-15 7:01
arunporwal4-Nov-15 7:01 
Questionquote Pin
Jhonnatan F.M25-Feb-15 7:16
Jhonnatan F.M25-Feb-15 7:16 
AnswerRe: quote Pin
Madhur Kapoor25-Feb-15 7:22
Madhur Kapoor25-Feb-15 7:22 
Questionwhat is the point no 5 - string manipulation : What is the difference Pin
dev463425-Feb-15 5:08
dev463425-Feb-15 5:08 
AnswerRe: what is the point no 5 - string manipulation : What is the difference Pin
TechJosh25-Feb-15 6:23
TechJosh25-Feb-15 6:23 
QuestionCode readability is the key point Pin
Keia25-Feb-15 3:24
professionalKeia25-Feb-15 3:24 
GeneralRe: Code readability is the key point Pin
PIEBALDconsult25-Feb-15 3:30
mvePIEBALDconsult25-Feb-15 3:30 
QuestionNull Conditional Operator is not good Pin
Cheung Tat Ming24-Feb-15 22:57
Cheung Tat Ming24-Feb-15 22:57 
GeneralRe: Null Conditional Operator is not good Pin
Evil Iceblock26-Feb-15 23:18
Evil Iceblock26-Feb-15 23:18 
GeneralBefore and after for conditional operators are completely different. Pin
Paulo Zemek24-Feb-15 12:19
mvaPaulo Zemek24-Feb-15 12:19 
GeneralRe: Before and after for conditional operators are completely different. Pin
Madhur Kapoor24-Feb-15 16:43
Madhur Kapoor24-Feb-15 16:43 
Thanks for pointing. Will have a look at fix that. Smile | :)

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.