Features of C# 7.0

cover

 

 

 

 

C# 7.0 Features

Last article we have looked the features of C# 6.0, so in this article we will see the features of C# 7.0

C# 7.0 March 2017 .NET Framework 4.6.2 Visual Studio 2017

out Keyword

The out parameter can be used to return the value in the same variable and as a parameter of the method, if any changes made to that parameter it will reflect the changes to that out variable.

In earlier version

old_Out

In C# 7.0

Here no need to declare it separately, specify directly in the argument itself.

New_Out1

Tuples

C# tuples as richer anonymous types, it’s a set of elements.

Old-Tuple

You can access name and age by referencing Item1 and Item2.

In C# 7.0

Here we can directly get the name and age instead of Item1 and Item2

New_Tuple

Discards

Discards are equivalent to unassigned variables, that variable may not even be allocated storage

They don’t have names, instead, they are represented as a _ (underscore.)

Discards

 

 

 

 

 

Pattern Matching

It’s not new to C#, in the earlier version itself introduced but in the coming version its added some more features in the implementation.

  • Pattern matching has the ability to extract the data from the expression.
  • Pattern matching can be used with any data type including custom whereas if/else can only be used with primitive types.

In C# 7.0, introduced 2 more features like “Is expression” and “Switch case statement”

With “Is” pattern

Is_Pattern

Pattern with Switch…case statement

Now Switch…case statement also works with expression as well.

Switch_Pattern

Ref local and returns

Ref Locals

Before C# 7.0 it was not possible to declare the return type with the ref modifier. While this feature was available using IL code, the feature was not reflected with C#. This changes with C# 7.0. However, before looking into this, let’s get into another new C# 7.0 feature: ref locals. Local variables can be declared with the ref modifier. Here, the variable x1 references variable x, and thus changing x1 changes x as well

Ref_Locals1

 

 

 

output: local variable x after the change: 2

Ref Returns

The ref keyword can also be used with the return type. This allows code as shown in the following code snippet. Here, an array of type int is declared and initialized. In the next line, a local ref variable is declared that references the first element of the array. This variable is then returned in the last statement of the method

Ref-Returns

 

 

 

 

Local Functions

Local functions enable you to define a function within the scope of another method to help in promoting encapsulation and bring local variables into scope.

LocalFunction

 

 

 

 

 

 

 

 

 

More expression-bodied members

Expression-bodied methods was introduced with C# 6.0, that simplify the syntactic expression for methods in C#. We have seen this for Methods and Properties in the previous version of C#. C# 7.0 extend this features for several new members including constructor, destructor, property assessors etc.

In earlier version

MoreExpressionBodies1

 

 

 

 

 

In C# 7.0

NewExpressionBodyFunctionMember1

 

 

 

 

 

Throw expression

C# 7.0 introduces throw expressions. We can add exception throwing to expression-bodied members, null-coalescing expressions and conditional expressions. This blog post introduces throw expressions, demonstrates how to use them and also provides a peek behind a compiled throw expression.

Throw expression

Generalized async return type

Running a Task from async methods can introduce performance bottlenecks in certain paths.

Ever since C# 5.0 when the async/await pattern was introduced, the only supported return types were Task<TResult>, Task, and void

Generalized asyn return

If the directory is empty, the known space is 0 and there is no need for an asynchronous thread to calculate the size.  However, since Task<long> is the return, it still needs to be instantiated.

By using ValueTask<….> we don’t want to instantiate the Task<long>, it can return Task or Task<long> and so on.

New_Generalized asyn return

Numeric literal syntax improvements

The digit separator “_” can be used inside number literals now. The purpose of a digit separator is to improve readability, nothing more

public const int One = 0b0001;

public const int Two = 0b0010;

public const int Four = 0b0100;

public const int Eight = 0b1000;

public const int Sixteen = 0b0001_0000;

Note: 0b -> indicates as binary

 

Conclusion

I hope this article will help much to understand the features of C# 7.0, in the next article we will see the features of C# 7.1 and 7.2

Leave a Reply

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