New features of C# 6


Visual Studio 2015 is released and it’s time to talk about Visual Studio 2015 features and about related to it features. And in this post I decided to talk about C# 6.0 as about a programming language. So I am not going to discuss Roslyn and some editor features – just new language features. All these features are already available and you can use them right now. Let’s start.

Default value for property

The simplest feature of C# 6.0 is ability to assign default value to auto properties.

class Cat
{
public ConsoleColor Color { get; set; } = ConsoleColor.Black;
}

Non-auto implemented properties cannot have initializer like this.

Getter-only auto properties

The next feature of C# 6.0 relates to a setter method of property. There are many scenarios when you don’t want to make a public setter but you still want to make an auto implemented property. In the previous version of C# you could make something like this:

public ConsoleColor Color { get; private set; }

But this approach doesn’t block ability of using the setter in other methods of the class but it can violate some business rules. That’s why C# 6.0 introduces a way to use properties with getters only:

public ConsoleColor Color { get; }

You can initialize properties like this in constructors or using a default initializer:

public ConsoleColor Color { get; } = ConsoleColor.Black;

But you cannot change getter-only properties in other methods of the same class.

using static

Everybody knows that keyword using allows to declare namespaces which compiler uses to identify the full name of types in your code. Thanks to using you can avoid full names for types like System.Net.Mail.SmtpClient. Instead you can use SmtpClient if you declare System.Net.Mail with using.

using static allows to do the same thing but for static methods rather than types. It allows to use static methods even without class name similar to global functions. For example we can create code like this:

using static System.Math;
using static System.Console;

class Program
{
static void Main(string[] args)
{
double angle = PI / 2;
WriteLine("sin(x)={0}", Sin(angle));
}
}

We used two using static constructions and thanks to that we can use PI, Sin and WriteLine static methods without explicit class name specification.

Interpolated string

In the previous code we used formatting approach which allows us to build output string using parameters. Of course, if you have lots of parameters it’s easy to make a mistake there: to declare wrong indexes for parameters, to miss some parameters or to specify parameters in the wrong order. Thanks to C# 6.0 I can inject parameters directly to a string. For example, my WriteLine method looks like:

WriteLine($"sin(x)={Sin(angle)}");

You can see that we used $ symbol in order to declare the string with the parameters inside. Once we specify $ we continue to use parametrized string but instead of indexes we use expressions directly. And you can continue to use formatting symbols:

WriteLine($"sin(x)={Sin(angle):n3}");

Expression-body (bodied) methods

I don’t know why but the next feature is scaring me. Let’s declare the following method:

public void PrintSin(double angle)
{
WriteLine($"sin(x)={Sin(angle)}");
}

You can see that this method contains just single expression. Using C# 6.0 we can rewrite this method in the following way:

public void PrintSin(double angle)=>WriteLine($"sin(x)={Sin(angle)}");

nameof

Even after applying of expression-body method, interpolated string and using static we still can use one more new feature of C# 6.0 there – it’s nameof operator.

public void PrintSin(double angle)=>WriteLine($"sin({nameof(angle)})={Sin(angle)}");

nameof allows to return the name of a variable. It’s very useful in case when you hardcoded names of the variables in debug messages, log messages etc. Thanks to nameof you can easy apply refactoring practices to your variables.

Operator “?”

The next feature is very useful for all types of applications. Let’s see the following code

class Program
{
static JustClass obj;

static void Main(string[] args)
{
obj = new JustClass() { data = 10 };

if ((obj!=null)&&(obj.data!=null)&&(obj.data.DataValue>3))
{
WriteLine("a very complex if");
}
}

class JustClass
{
public Data data { get; set; } = null;
}
class Data
{
public int DataValue { get; set; }
}
}

Pay special attention to the if statement. Even in this easiest case we have a very complex if. There are three conditions and two of them test value for null. In C# 6.0 we can simplify this code:

if (obj?.data?.DataValue > 3)
{
WriteLine("a very complex if");
}

It’s the same code but we simplify null conditions using ? operator. You cannot use this operator if you have just one condition. In other words, right after this operator there should be .<data field> but it is still possible to make lot’s of if operators more clear.

Indexer initializer

Just run this code:

class Program
{
static JustClass obj;
static void Main(string[] args)
{
obj = new JustClass() {10 = 5,[1] = 7 };
Console.ReadLine();
}

class JustClass
{
public int this[int i]
{
get
{
return i;
}
set
{
Debug.WriteLine(value.ToString());
}
}
}
}

You can see that class JustClass has an indexer inside. In C# 6.0 developers can use indexers in order to initialize objects. For example, in my code I initialized element with index 10 and index 1. Of course, my example doesn’t have much sense but the idea should be clear.

Conditional exceptions

Finally, let’s play with the following code:

static void Main(string[] args)
{
try
{
throw new Exception("File not found");
}
catch(Exception ex) when (ex.Message.Contains("File"))
{
throw ex;
}
catch (Exception ex)
{
throw ex;
}
}

You can see that thanks to when keyword we can catch exceptions based on results of expressions.

That’s all about language. Next time I will write about IDE and some features there.


Comments (0)

Skip to main content