7 New Cool Features in C# 6.0

.NET
Share
7 New Cool Features in C# 6.0

Here you will find seven new cool features of the new version of the C# language – C# 6.0 or a.k.a. C# vNext.
You can get them by downloading Visual Studio 2014 or installing the Roslyn package for Visual Studio 2013 from here.

What is new in C# 6.0?

  • ­Auto-property Enhancements
  • ­Primary Constructors (dropped)
  • ­Expressions as Function Body
  • ­Using Static
  • ­Exception Filters
  • ­Declaration Expressions (inline declarations)
  • ­Nameof Expressions
  • ­Null-conditional Operators
  • ­Index Initializers
  • ­Await in catch/finally
  • ­Not Implemented, Yet

1. Auto-property Enhancements

You can initialize auto-property, the same way you can do to a field.

public class Person
{
public string FirstName { get; set; } = Anton;
public string LastName { get; set; } = Angelov;
}

 2. Parameters on Classes and Structs

Using a primary constructor in C# 6.0.

public class Person(string firstName, string lastName)
{
public string FirstName { get; set; } = firstName;
public string LastName { get; set; } = lastName;
}

You can also add validations or other code to the primary constructor.

public class Person(string firstName, string lastName)
{
{
if (firstName == null)
throw new ArgumentNullException(firstName);
if (lastName == null)
throw new ArgumentNullException(lastName);
}
public string FirstName { get; } = firstName;
public string LastName { get; } = lastName;
}

 3. Using Static

Now you can access all static members without a qualification.

using System.Console;
namespace ConsoleApplicationTest
{
class Program
{
static void Main(string[] args)
{
//Use writeLine method of Console class
//Without specifying the class name
WriteLine(Hellow World);
}
}
}

 4. Exception Filters

if the conditional expression evaluates to true, then the catch block is run.

try
{
throw new Exception(C#);
}
catch (Exception ex) if (ex.Message == C#)
{
// this one will execute.
}
catch (Exception ex) if (ex.Message == VB.NET)
{
// this one will not execute
}

The feature is already available in VB.NET and F#.

5. Null-conditional Operators

You can access members and elements only when the object is not null.

int? length = animals?.Length; //null if animals is null
Animal first = animals?[0]; //null if animals is null

Together with the null coalescing operator ??:

int length = animals?.Length ?? 0; // 0 if animals null

6. Index Initializers

New syntax to initialize a index collections.

Old way (C# 5.0)

var animals = new Dictionary<int, string> {
{ 5, cat },
{ 9, dog },
{ 8, rat }
};

New way (C# 6.0)

var animals = new Dictionary<int, string> {
[5] = cat,
[9] = dog,
[8] = rat
};

7.  $ sign

The new way to initialize index objects and access them.

var names = new Dictionary<string, string>()
{
// using inside the intializer
$first = Anton
};
//Assign value to member
//the old way:
names[first] = Anton;
// the new way
names.$first = Anton;

You can find more information on the following URL: http://roslyn.codeplex.com/discussions/570292
Also you can find a detailed presentation and a video lecture on my colleague’s (Niki Kostov) blog.
The post image was downloaded from: http://www.veooz.com/photos/cHFn1MZ.html