Click here to Skip to main content
14,694,397 members
Articles » Languages » C# » General
Technical Blog
Posted 24 Feb 2015

Tagged as

Stats

22.6K views
18 bookmarked

C# 6.0 New Features

Rate me:
Please Sign up or sign in to vote.
3.92/5 (21 votes)
24 Feb 2015CPOL
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

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

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

C# 6

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

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

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

C# 6

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:

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.

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

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

C# 6

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

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

C# 6

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.

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

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

C# 6

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)

Share

About the Author

Madhur Kapoor
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 8:06
Memberarunporwal4-Nov-15 8:06 
SuggestionWe can't use System.Console without static keywork Pin
arunporwal4-Nov-15 8:01
Memberarunporwal4-Nov-15 8:01 
Questionquote Pin
Jhonnatan F.M25-Feb-15 8:16
MemberJhonnatan F.M25-Feb-15 8:16 
AnswerRe: quote Pin
Madhur Kapoor25-Feb-15 8:22
MemberMadhur Kapoor25-Feb-15 8:22 
Questionwhat is the point no 5 - string manipulation : What is the difference Pin
dev463425-Feb-15 6:08
Memberdev463425-Feb-15 6:08 
AnswerRe: what is the point no 5 - string manipulation : What is the difference Pin
TechJosh25-Feb-15 7:23
MemberTechJosh25-Feb-15 7:23 
QuestionCode readability is the key point Pin
Keia25-Feb-15 4:24
professionalKeia25-Feb-15 4:24 
GeneralRe: Code readability is the key point Pin
PIEBALDconsult25-Feb-15 4:30
professionalPIEBALDconsult25-Feb-15 4:30 
QuestionNull Conditional Operator is not good Pin
Cheung Tat Ming24-Feb-15 23:57
MemberCheung Tat Ming24-Feb-15 23:57 
GeneralRe: Null Conditional Operator is not good Pin
Evil Iceblock27-Feb-15 0:18
MemberEvil Iceblock27-Feb-15 0:18 
GeneralBefore and after for conditional operators are completely different. Pin
Paulo Zemek24-Feb-15 13:19
professionalPaulo Zemek24-Feb-15 13:19 
GeneralRe: Before and after for conditional operators are completely different. Pin
Madhur Kapoor24-Feb-15 17:43
MemberMadhur Kapoor24-Feb-15 17:43 

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.