.netASP.netC#.net

New Features of C# 6.0

In this article, we will learn the top new features and enhancements introduced in Microsoft C# 6.0.

Auto Property Initializer:

This feature enables us to set the values of properties where they are declared.
Previously, we use constructor to initialize the auto properties to non-default value but with this new feature in C# 6.0, it doesn’t require to initialize these properties with a constructor as shown below:

We can use this property with getter/setter and getter only.Using getter only help achieve immutability.

Exception Filters:

Microsoft introduced this CLR feature in C# with version 6.0 but it was already available in Visual Basic and F#. To use Exception Filters in C#, we have to declare the filter condition in the same line as that of the catch block and the catch block will execute only if the condition is successfully met as shown below:

Remember that Exception Filter is a debugging feature rather than a coding feature. We will discuss more on this in next post.

await in catch and finally block:

We frequently log exceptions to a log file or a database. Such operations are resource extensive and lengthy as we would need more time to perform I/O. In such circumstances, it would be great if we can make asynchronous calls inside our exception blocks. We may additionally need to perform some cleanup operations in finally block which may also be resource extensive.

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:

String Interpolation

string.Format was used till today to generate the FullName value in the Customer class.A new feature named string interpolation provides a large improvement in this area. Rather than filling placeholders with indexes provide an array of values to be slotted in at runtime, you provide the source of the parameter value instead, and the string.Format call is replaced by a single $ sign. This is how the FullName property declaration looks when using string interpolation in the expression body instead:

Apart from this saving a number of key strokes, it should also minimise (if not remove) the possibility of FormatExceptions being generated from inadvertently supplying too few values to the argument list.

Dictionary Initializer:

C# 6.0 introduces more cleaner way for dictionary initialization as follows:

 

Previously it was declared like below:

Null – conditional Operator:

The null-conditional operator “?” uses one question mark.
It can be used to reduce the no. of lines in code file and provide an easy access to check for Null and return the result.
The null conditional operator tests for null before accessing a member of an instance.

Example:

Output:1
2
True
True

© 2015, www.techkatak.com. All rights reserved.

4 thoughts on “New Features of C# 6.0

  1. Null coalescing ‘??’ is not new, it’s been there a long time ago. The new feature of c# 6.0 is the null-conditional operator which checks the if the variable is null in the statement call chain.

    For example:

    return item?.Trim();

    Here if the item is null it will not call the .Trim() function thus will not throw an exception, but if the item is not null it will continue to the call chain.

Comments are closed.