You are here

What's New in C# 6.0

Submitted by Asif Nowaj, Last Modified on 2019-11-22

Today I'll try to jot down some of the new language features included in C# 6.0

Remind Old day's
C# 1.0 (2002)
The first release of C# language was in 2002. While I am writing this article, C# is 13 years old.

C# 2.0 (2005)
In 2005, C# 2.0 is released with new language features like Generics, Nullable types, Static Classes, Delegate Interface.

C# 3.0 (2007)
In 2007, C# 3.0 came along with language integrated query features including lamda expression, query expression, explicit typing.

C# 4.0 (2010)
In 2010, C# 4.0 is released with dynamic binding, Named and optional arguments.

C# 5.0 (2012)
In 2012, C# 5.0 gives us the async and await keyword, nowadays asynchronous programming is everywhere.

New Language Features of C# 6.0 (2015)

Auto property initializer
public class Employee
{
//Auto property initializer
public GUID Id {get; } = GUID.NEWGuid();
}

This allows us to write expression along side of property declaration and this can set the initial value to the property. Here we don't have to write the setter for initialization.

Import of static type members into namespace

using static System.Console;

class Program
{
public static void Main()
{
WriteLine("Hello World!");
}
}

The feature allows to use static type as like namespace. Before C# 6.0, we use to write

using System;

class Program
{
public static void Main()
{
Console.WriteLine("Hello World!");
}
}

Collection Initializer

Dictionary wonders = new Dictionary {
["One"] = "Taj Mahal",
["Two"] = "Nalanda",
["Three"] = "Konark Sun Temple"
};

Here we can use index initializer.

Await in Catch and Finally block
In C# 5.0, await keyword was not permissible in the catch and finally block. This will help in the scenario where things can go wrong.

try
{
do_operation();
await _logger.writelineAsync("Successful");
}
catch(exception ex)
{
await _logger.writelineAsync("Failed");
}
finally
{
await _logger.closeAsync("Failed");
}

Null Conditional Operator

We generally use null checking whenever required like below

int len = 0;
if (customers != null)
{
Len = customers.Length;
}

Now we can use

int? len = customers?.Length;

Null conditional operator can used together with the null coalescing operator ??

int length = customers?.Length ?? 0; // 0 if customers is null

Nameof operator

Occasionally a string needs to be provided that names some program element: when throwing an ArgumentNullException you want to name the guilty argument

Using string literals for this purpose is simple, but error prone. You may spell it wrong, or a refactoring may leave it stale. nameof expressions are essentially a fancy kind of string literal where the compiler checks that you have something of the given name, and Visual Studio knows what it refers to, so navigation and refactoring will work:

if (x == null) throw new ArgumentNullException(nameof(x));

Compiler as a service

'Roslyn' is the code name of the .NET compiler platform. This covers both C# and VB. Now the source code of the Roslyn is open. So you can download the compiler code and sneak into it and change if you want and compile your code with your modified compiler.

Go to https://github.com/dotnet/roslyn

Discussion or Comment

If you have anything in mind to share, please bring it in the discussion forum here.

https://forum.everyething.com/dotnet-f40/