PROWAREtech

articles » current » dot-net » c-sharp-6

.NET: What's New or Changed in C# 6

A guide to the changes and additions in C# version 6.

Improvements to C# are discussed at https://github.com/dotnet/csharplang. Here, find proposals to the language and submit them, too.

The C# 6 extensions are exception filters, string interpolation, dictionary initializers, expression-bodied methods and properties, nameof operator, await in catch, auto-property initializers, using static, the null conditional operator, and read-only auto properties.

Exception Filters

Exception Filters are clauses that determine when a given catch clause should be applied.

static void Main(string[] args)
{
	int[] array = { 1, 2, 3, 4, 5 };
	try
	{
		array[5] = 3;
	}
	catch(Exception e) when (e.Message.Contains("bounds")) // use when here
	{
		Console.WriteLine("out of bounds");
	}
}

String Interpolation

This is short for using the string.Format method which suffers from poor perfomance.

int a = 3, b = 4;
string result = $"the result of {a} x {b} is {a * b}"; // equals "the result of 3 x 4 is 12"

Dictionary / Index Initializers

This really is not very special, but before C# 6:

Dictionary<int, string> statuses = new Dictionary<int, string>
{
    { 302, "Page moved"},
    { 404, "Page not Found"},
    { 500, "Internal server error"}
};
string description = statuses[500]; // description equals "Internal server error"

And now with C# 6, assign using the index into the collection:

Dictionary<int, string> statuses = new Dictionary<int, string>
{
    [302] = "Page moved",
    [404] = "Page not Found",
    [500] = "Internal server error"
};
string description = statuses[500]; // description equals "Internal server error"

Read-only Auto-properties

This is to geared toward immutable types. Declare the auto property with a get accessor.

class Business
{
	public string Name { get; } // can only be set in the constructor
	public double Price { get; } // can only be set in the constructor
	public Business(string name, double price) // constructor
	{
		Name = name;
		Price = price;
	}
}

Expression-bodied Methods and Properties

This works for read-only properties and methods. This example uses string interpolation and read-only auto-properties

class Business
{
	public string Name { get; }
	public double Price { get;  }
	public string NameAndPrice => $"{Name} ${Price}"; // read-only property
	public override string ToString() => $"{Name} ${Price}"; // method
	public Business(string name, double price)
	{
		Name = name;
		Price = price;
	}
}

nameof Operator

There is no magic here worth explaining. Entering nameof(BusinessName) is better than simply entering "BusinessName".

var BusinessName = "None";
string varname = nameof(BusinessName); // varname equals "BusinessName"

await in catch and finally

await can now be used in catch and finally blocks.

Auto-property Initializers

This declares an initial value for an auto-property as part of the property declaration.

class Business
{
	public System.Collections.Generic.List<string> Services { get; } = new System.Collections.Generic.List<string>();
}

using static

This enables the importing of static methods of a class.

using static System.String;
using static System.Math;

null conditional operator

This operator (?) makes checking for null much easier. Replace the . with ?. or the [] with ?[].

class Business
{
	public string Name { get; } = "";
}
Business bus = null;
var busname = bus?.Name; // because bus is null, busname is set to null and no exception is thrown
var busname2 = bus?.Name ?? "undefined";

Business[] busarray = null;
busname = busarray?[0]?.Name; // no exception can possibly be thrown

This site uses cookies. Cookies are simple text files stored on the user's computer. They are used for adding features and security to this site. Read the privacy policy.
CLOSE