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();
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)
{
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.