Top 10 C# 6.0 Language Features

Static Using Syntax:

Before C# 6.0, In a console application if you wanted to print something, then you would write the following line of code:

In C# 6, you can now add the using static qualifier and reference the WriteLine method by itself as shown below:

This will also work for custom classes created by yourself. Let’s look at the below example.

Since you have declared CustomClass with the static keyword, you can run the method by just calling the method name. The output for this sample program would be :

2. String Interpolation :

String Interpolation is a mechanism to concatenate two or more strings together.In older versions of C#, we were using “+” operator or String.Format method to concatenate strings but in C# 6.0, Microsoft has provided a feature named String Interpolation to concatenate strings.

Using “+” Operator:

Using String.Format method:

New ways of String Concatenation in C#6.0

The output for this program will be:

3. Exception Filters:

Earlier Exception filters were supported in Visual Basic but not in C#. They allow you to specify a condition for a catch block.

In C#6.0, without entering the catch block, you can check to see which condition met your exception.

4. Await in a Catch and Finally Block:

In C# 6, now you can write asynchronous code inside a catch/finally block. This will help you to log exceptions to a file or database without blocking the current thread. Let’s look at below example:

5. Null Conditional Operator:

Most of us do not like NullReferenceException . Let’s look at below example, on how to handle NullReferenceException prior to C#6.0.

If you enter some data into name, then the console prints out that name else this returns “Field is null”.

New ways to handle NullReferenceException in C#6.0:

In C# 6.0, you can use ?. to check if an instance is null or not. Let’s look at below example to see how it works in C#6.0.

6. Auto Property Initializer:

Prior to C#6.0, we were using constructor to initialize the auto properties to non-default value.

In C#6.0, you can modify the Employee class and populate the property called EmployeeID with inline initialization.

The output for this sample will be a random guid generated by the system.

7. Dictionary Initializers:

In C# 5, you would initialize the Dictionary with a {“Key”, “Value”} Pair. Let’s look at below example to understand it better.

In C# 6, you can place the key between two square brackets [“Key”] and then set the value of the key [“Key”] = “value”.

8.Expression Bodied Function & Property:

Expression Bodied Functions are functions with no statement body. Instead, you implement them with an expression following the function declaration

The result of the above program would be 10.

9. Getter-only Auto Properties:

When you use auto implemented properties in C# 5 and lower, you must provide a get and set. If you want the property value to be immutable, you can use the private accessor on the setter. With C# 6, you can now omit the set accessor to achieve true readonly auto implemented properties:

10. NameOf Expression:

Name of Expression is a new feature introduced in C#6.0.In Enterprise level applications, we handle exceptions at different levels.Showing a specific type name with an error message can be an easy way to find the code block where the exception just occurred. But by adding this we can’t show it to user because the type name can be changed anytime while refactoring but hard coded string won’t change accordingly.

Before C# 6.0 :

What happens if someone changes the Title variable to newTitle? Since we hard-coded the “Title is null” error, that is what will appear to the end-user. We would rather it now say, “newTitle is null”, but the compiler cannot recognize this.

In C# 6.0, we can refactor our code to remove the string literals and use the nameof expression.

Summary:

In this article, we have discussed top 10 new features introduced in C# 6.0.
– Auto Property Initializer
– Await in a Catch and Finally Block in C#6.0
– Dictionary Initializers in C# 6.0
– Exception Filters in C#6.0
– Expression Bodied Function & Property in C#6.0
– Getter-only Auto Properties in C#6.0
– NameOf Expression in C#6.0
– Null Conditional Operator in C#6.0
– Static Using Syntax in C#6.0
– String Interpolation in C#6.0

Thanks For visiting !!

Leave a Reply

Your email address will not be published. Required fields are marked *