Brace yourself.. C# 6.0 is coming!! or has it already arrived?

Annnd .. you'll love it.

Version 6.0 has been out since April via the new .NET compiler, Roslyn and will soon ship with Visual Studio 14 but To help you find its awesomeness here are a few highlights.

Null Propergation operator

Probably this was the feature you were waiting for all along The Null propergation operator ( ?. ). lets assume you have an object (Employee) which holds a property which is another class object (Department) which in turn holds a string property (Name) and your objective is to assign a value to this string  property imagine how ugly this code looks

Before :
if(Employee != null && Employee.Department != null)
{
    Employee.Department.Name = "Software Development";
}

This looks ugly. so many conditional checks to make sure we do not face the null object exception.

With C# 6.0 you do it in this awesome way.

Now: 

Employee?.Department?.Name = "Software Development";


No "if" conditions for null checks anymore and you can assign the value , All you have to do is use the Null Propergation operator (?.) on the object to find out if its not null and assign the value.

How cool is that ?!  This is not limited to properties only, this works even on methods.

Now: 

string departmentName =  Employee?.Department?.GetName();


Thinking of disposing an object..?

Now: 

Employee?.Dispose(); 


Employee object wont be disposed if its null  and if its not null, gone into the garbage can!


Auto Property Initializer and Primary Constructors

Remember we could assign(initialize) a value to a variable as and when we declare it but we could not do the same for the property declaration? well that is a thing of the past now and would it not be nice if you could declare the default constructor in a simpler way? well there is a way now for that as well.

Before:
public class Employee
{
    public string Name{ get; set;}
    public string Department{ get; set;}

    public Employee(string name , string department)
    {
         Name = name;
         Department = department;
    }
}


Now: 
//Primary constructor need not be declared again
//Provide the parameter list in the class declaration.

public class Employee (string name , string department)
{
    //Properties declared and assigned at the same time.
    public string Name{ get; set;}       =  name;
    public string Department{ get; set;} =  department;
}

Please note: C# is making us LAZY!!


Exception Filtering now made easy

Imagine you have no clue what type of exception you are going to come across and you will write a generic catch block. But however you have to run some specific statements based on the error code?

well you would do it the following way

Before:
try
{
   //Try to do something and BAM!! an exception
}
catch (Exception ex)
{
   if (ex.ErrorCode == 12345)
   // do something

   throw;
}

well  now if can filter this as below.

Now: 
try
{
   //Try to do something and BAM!! an exception
}
catch (Exception ex)  if (ex.ErrorCode == 12345)
{
   // do something
}

looks cleaner doesnt it?


Declaring the variables inline

Now we can declare variables inline and can still be used outside. how? refer below.


Before:
int emp_no ; // emp_no declared outside

if (!Int32.TryParse("100", out emp_no ))
{
    // do something with emp_no 
}

Now:
if (!Int32.TryParse("100"out int emp_no )) // emp_no declare inline
{
    // do something with emp_no 
    // the scope for emp_no exists here.
}


All this and more in the C# v 6.0 .
Do note that with the Roslyn compiler you can still get all the above and more benefits in your existing projects.

There is a lot more .. please visit http://msdn.microsoft.com/en-us/magazine/dn683793.aspx

For the FAQs on Roslyn please follow http://blogs.msdn.com/b/csharpfaq/archive/2014/04/03/taking-a-tour-of-roslyn.aspx

3 comments: