C# 6.0
C# 6.0

C# 6.0 has been released with Visual Studio 2015. It introduces some of the new feature in C#. Below are the main features introduced in C# 6.0.

  • Compiler-as-a-service (Roslyn)
  • Import of static type members into namespace
  • Exception filters
  • Await in catch/finally blocks
  • Auto property initializers
  • Default values for getter-only properties
  • Expression-bodied members
  • Null propagator (null-conditional operator, succinct null checking)
  • String Interpolation
  • nameof operator
  • Dictionary initializer

Let’s take a quick overview of this feature.

Compiler-as-a-service (Roslyn)

Visual Studio 2015 now comes with Built in Compiler system namely Roslyn.

But what difference it makes to developers?

Simple answer is now you can do more with your code than ever before.

But How?

Till date compiler comes as black box which takes source code as a the input and generate object code. Csharp compiler was also doing the same. Take C# source code and convert it into Intermediate Language code (IL) which will be further passed to the JIT.

But all this happen in black box so you don’t get any information at all unless you see the output. Roslyn changed this. Roslyn converted this black box to white box. With Roslyn you can get detail from each of the stage in compilation pipeline and use it the way you want.

Some of the interesting thing you can do with Roslyn are Code Analysis Code Refactoring. You can generate various code matrix to check the health of code.

Import of static type members into namespace

You can now import static member with the using keyword. Let’s understand it by example. Below is very simple code with use console’s writeline() static method.

using System;


namespace ConsoleApp1
{
    public class Program
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Hello");
            Console.ReadLine(); 
        }
    }


}

You can now import static names with the help of using static keyword. Our program will looks more simpler like below by making use of using static on console. We need not to write console every place. It is much more readable compare to previous code.

   

using System;
using static System.Console; 

namespace ConsoleApp1
{
    public class Program
    {
        public static void Main(string[] args)
        {
            WriteLine("Hello");
            ReadLine(); 
        }
    }

}

Please read my Next article on this series on Exception Filters in C# 6.0 are here.

Planned but Removed Features from C# 6.0

Some of the feature which was announced and planned but finally removed from the C# 6.0.
Primary Constructor
Declaration expressions in C# / Out parameters in VB

Please follow and like us:
New features in C# 6.0

One thought on “New features in C# 6.0

  • January 5, 2017 at 7:20 am
    Permalink

    Nice…thanks for sharing it

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *