It seems like only yesterday we got C# 6, it all happens in the software development land. And now you are seeing C#7.0. Jeez, it is having more cool features than C#6.0 and damn sure after going through these you will also be on my side waiting for that one fine day.

This has none of the disadvantages of out-parameters and the tuple type, but it’s rather verbose and it is meaningless overhead. Because, the properties inside a (class / struct) are not coming under one roof and not having any common base characteristics. For the purpose of creating temporary data structure C# has provided Tuples.

All three ways mentioned above has their own disadvantages, so they want to overcome these shortcomings by introducing a miracle.

Multiple return types in C# 7.0

Tuple return types:

You can specify multiple return types for a function, in much the same syntax as you do for specifying multiple input types. These are supposed to be called Tuple Types.

The syntax (double topGrossSalary,string hrName) indicates an anonymous struct type with public fields of the given names and types. Note that this is different from some notions of tuple, where the members are not given names but only positions (i.e Tuple<double, string>). This is a common complaint, though, essentially degrading the consumption scenario to that of System.Tuple above. For full usefulness, tuples members need to have names. This is also fully compatible with async.

In the above example we have created a method with return type of tuple type with known target type and constructed a tuple type inline using the previously declared individual variables (i.e. return(s, c)).

Using named arguments as a syntax analogy it may also be possible to give the names of the tuple fields directly in the literal:

We don’t need to the tuple object as a whole because it doesn’t represent a particular entity or a thing, so the consumer of a tuple type doesn’t want to access the tuple itself, and instead he can access the internal values of the tuple.

Instead of accessing the tuple properties as in the example of Tuple Return Types, you can also de-structure the tuple immediately:

The “is” operator can be used to test an expression against a pattern. As part of the pattern-matching feature repurposing the “is” operator to take a pattern on the right-hand-side.

relational_expression : relational_expression 'is' pattern;

It is a compile-time error if the relational_expression to the left of the “is” token does not designate a value or does not have a type. Every identifier of the pattern introduces a new local variable that is definitely assigned after the “is” operator is true (i.e. definitely assigned when true).

Pattern

Patterns are used in the is operator and in a switch_statement to express the shape of data against which incoming data is to be compared.

There are many areas where we can use patterns in c#. You can do pattern matching on any data type, even your own, whereas if/else you always need primitives to match. Pattern matching can extract values from your expression.

In the above sample for holding the objects, I need to create temporary variables s and t. So if I have n number of objects I need to create N temporary variables with distinct names unnecessarily, which make my code more verbose. And also the temporary variables are only need for a particular code block but it is having scope throughout the function. Note the need to declare variables s and t ahead of time even though it is used in one of the code blocks.

As part of the pattern-matching feature we are repurposing the “is” operator to take a pattern on the right-hand-side. And one kind of pattern is a variable declaration. That allows us to simplify the code like this,

Now you can see that the temporary variables s and t are only declared and scoped just to the place they need to be. The switch statement is also repurposed like the case branches can also have patterns instead of just constants.

The class will automatically implement Equality implementations like (such as GetHashCode, Equals, operator ==, operator != and so forth).

A default implementation of ToString() method.

Non-Null able reference types:

Non- nullable reference option will let you create a reference type that is guaranteed not to be null. NullReference expections are too common in a project. Often we developers forgot to check a reference type for null before accessing the properties of it, thus paving way to problems.

Either we forget check for it making our code vulnerable to runtime exceptions or we will check for it which makes our code more verbose.

Instead of using the “?” for identifying the nullable value type we are going to use “!”.The currently proposed syntax is as follows:

An immutable object is an object whose state cannot be changed after its creation, which means Immutable objects are objects which once loaded cannot be changed / modified by any way external or internal.

Immutable objects offer few benefits,

Inherently thread-safe.

Easier to parallelize.

Makes it easier to use and reason about code.

Reference to immutable objects can be cached, as they won’t change.

Currently it is also possible to create immutable classes. Create a class with properties only with get and read-only and constant private variables.

Code in the above example is definitely an immutable class, but the intent of the class is not clearly stated as immutable. Because, in future any one can add setters to any of the properties thus making it as mutable.

The proposed syntax for creating an immutable class will force the developer to strictly adhere the rules hence will make the class an immutable. Below is the proposed syntax,