Part 1: C#7 features. What is new?

In this article I will go through the changes/ advancement of C# over the time.

New features in C# 7.0

The topics I will cover the following topics

  • Out Variables
  • Pattern Matching
  • Tuples
  • Local Functions
  • Ref Returns & Locals
  • Expression Bodied Members
  • Throw Expressions
  • Generalized Async Return Types

Out Variable

Previously

That means it was needed to declare before to put value to a variable

but now

If you notice the last line which is out of scope of the if statement, it also not just available in the scope of if but also outside the scope.

Another interesting thing is that if we use the var instead of DateTime then it also works like following

But what if it fails?

it is obvious that the if block will fail but that new variable has been used outside the scope as well.

The answer is it will show the default value as like following output. And it will be “null” if it a reference type.

Pattern Matching

C# is not enriched with pattern matching that much. That is the kind of thing with F# but let us explore what we have in C#

Lets’s consider the following classes:

And if a method is called with “shape” reference as parameter like following, we have following option to identify the type of object

or can use following way

But that can be easily written in following way

A new way of casting things

Using the switch we are also about to find out the object type and can consider the specific check point at the same time like following

Tuples

Tuples have already existed in C# base class library as a purely library solution without any kind of change to C# Syntax.

Lets see a old way of using a Tuple

The old way of Tuple only consider a predefined variable to get the returned value only as Item1, Item2…….. etc. But it was difficult to identify the type in a enterprise level application

So the new way of doing it is as below

But it is also possible to use this in following way which is like exactly the old way with little bit of syntax changed if some one is comfortable with but not recommended to

And if we use a Func, we can do like the following

Output will be as excepted.

Deconstruction or multi variable declarations

It is not only for the tuple but also multi variable can be fetched using deconstruction method

say we have a following class

now if we want to get the properties of “Human” class like the way we are getting value from Tuple? Here is a way

But the above line would not make any sense because there is a need of “deconstruct” to make this thing work like as excepted. In the class we need to have following Deconstruct method.

So the total class will be

And in the main method simply we can use it like the following

And if we do not need some variable we simply can use the “_” to skip getting the value like the following

So the age will not be available to output and other will be okay as it is.

Local Functions

It already existed previously. So let’s see how local functions were working before C#7

In simpler a local function can be used from inside a local function.

Ref Returns & Locals

Another new feature is referencing to local variable.

Say we have an array of a int

and the following line of code

assign the 3rd value of “myInt” to “myVar”.

changing the value of myVar  will change the 3rd value of “myInt”.

And here is the output

but this will not work for the “List” though

A small hack

What will happen if we downsize the array after assigning the value to “myVar”?

It will still keep the same value even after the array is downsized.

If we see the following code

This function return the value of an array from a specific position. and in the main method

we can get the value by the following code

It will show following output

if we just write following

then run removing the “myValue”, it will show the following output

Expression Bodied Members and the later topics are discussed at the 2nd part.

Leave a Reply

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