1) async / await



2) Object / array / collection initializers



//Just some demo class

public class Employee {

public string Name {get; set;}

public DateTime StartDate {get; set;}

}



//Create an employlee by using the initializer

Employee emp = new Employee {Name="John Smith", StartDate=DateTime.Now()};

Use the async / await-pattern to allow unblocking of the UI / current thread when execution blocking operations. The async / await-pattern works by letting the code continue executing even if something is blocking the execution (like a web request).Read more about the async / await-pattern here: https://msdn.microsoft.com/en-us/library/hh191443.aspx Create instances of classes, arrays and collections easily by using the object, array and collection initializers:

The above example can be really useful in unit testing but should be avoided in other contexts as instances of classes should be created using a constructor.



Read more about initializers here: https://msdn.microsoft.com/en-us/library/bb384062.aspx

3) Lambdas, predicates, delegates and closures

These features are practically a necessity in many cases (e.g. when using Linq), make sure to actually learn when and how to use them.



Read more about Lambdas, predicates, delegates and closure here: http://www.codeaddiction.net/articles/13/lambda-expressions-delegates-predicates-and-closures-in-c﻿

4) ?? (Null coalescing operator)

//May be null

var someValue = service.GetValue();

var defaultValue = 23



//result will be 23 if someValue is null

var result = someValue ?? defaultValue;

string anybody = parm1 ?? localDefault ?? globalDefault;

var totalPurchased = PurchaseQuantities.Sum(kvp => kvp.Value ?? 0);

The ??-operator returns the left side as long as it's not null, in that case the right side will be returned:The ??-operator can be chained:And it can be used to convert nullable types to non nullable:

Read more about the ??-operator here: https://msdn.microsoft.com/en-us/library/ms173224.aspx



5) $"{x}" (String Interpolation) - C# 6



//Old way

var someString = String.Format("Some data: {0}, some more data: {1}", someVariable, someOtherVariable);



//NewWay

var someString = $"Some data: {someVariable}, some more data: {someOtherVariable}";

A new feature of C# 6 that lets you assemble strings in an efficient and elegant way:

You can put C# expressions in between the braces, which makes this very powerful.



6) ?. (Null-conditional operator) - C# 6



//Null if customer or customer.profile or customer.profile.age is null

var currentAge = customer?.profile?.age;

7) nameof Expression - C# 6

public void PrintUserName(User currentUser)

{

//The refactoring tool might miss the textual reference to current user below if we're renaming it

if(currentUser == null)

_logger.Error("Argument currentUser is not provided");



//...

}

public void PrintUserName(User currentUser)

{

//The refactoring tool will not miss this...

if(currentUser == null)

_logger.Error($"Argument {nameof(currentUser)} is not provided");



//...

}

8) Property Initializers - C# 6



Property initializers lets you declare an initial value for a property:



public class User

{

public Guid Id { get; } = Guid.NewGuid();

// ...

}



The null-conditional operator works like this:No more NullReferenceExceptions!Read more about the ?.-operator here: https://msdn.microsoft.com/en-us/library/dn986595.aspx So the new nameof-expression might not seem important, but it really has it value. When using automatic re-factoring tools (like ReSharper) you sometime need to refer to a method argument by it's name:This is how you should use it...Read more about the nameof-expression here: https://msdn.microsoft.com/en-us/library/dn986596.aspx

A benefit of using property initializers is that you can not declare a set:er, thus making the property immutable. Property initializers works great together with C# 6 primary constructor syntax.



9) as and is-operators



if (Person is Adult)

{

//do stuff

}

SomeType y = x as SomeType;

if (y != null)

{

//do stuff

}



10) yield-keyword



public static IEnumerable<int> Power(int number, int exponent)

{

int result = 1;

for (int i = 0; i < exponent; i++)

{

result = result * number;

yield return result;

}

}

The is-operator is used to control if an instance is of a specific type, e.g. if you want to see if a cast is possible:﻿Use the as-operator to try to cast an instance to a class. It will return null if cast was not possible:The yield-keyword lets you feed an an IEnumerable-interface with items. The following example will return each powers of 2 up to the exponent of 8 (e.g. 2, 4, 8, 16, 32, 64, 128 ,256):yield return can be very powerful if it's used in the correct way. It enables you to lazily generate a sequence of objects, ie. the system does not have to enumerate the whole collection - it can be done on demand.