Quantcast
Channel: Bitshifter GmbH – Bitshifter GmbH – Blog
Viewing all articles
Browse latest Browse all 12

Useful C# 6.0 features

$
0
0

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;

//conventional...
if (p.Address.Street != null)
{
  result = p.Address.Street;
}
else
{
 result = "default value";
}

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

Nice, isn’t it? Happy coding!


Viewing all articles
Browse latest Browse all 12

Latest Images

Trending Articles





Latest Images