Useful C# 6.0 features

There are alot of new features in C# 6.0, some of them might help you write cleaner code in less time. Let’s have a look at two of the (in my opinion) best features:

Interpolated Strings
We have all been there.. concatenating strings usings tons of “+” or using String.Format, just to use some kind of placeholders. Interpolated strings are a neat way to get rid of that.

Employee emp = new Employee();
emp.Firstname = "John";
emp.Lastname = "Doe";

//enable the interpolated string by using a $ prefix
Console.WriteLine($"Firstname: { emp.Firstname }, Lastname { emp.Lastname }");

Null-conditional operator
Checking for a null value in a complex type before accessing its properties is a common task. Unfortunately, this requires a few lines of code. C# 6.0 introduces null-conditional operators.

string result = null;

if (p.Address.Street != null)
  result = p.Address.Street;
 result = "default value";

//null-conditional operator
result = p.Address?.Street ?? "default value";

Nice, isn’t it? Happy coding!

Useful C# 6.0 features

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s