Feature List in C# 7.0

  1. Local functions
  2. Tuple Types and literals
  3. Record Types
  4. Pattern matching
  5. Non Nullable reference types
  6. Immutable types

Local Functions

Local functions would have the same capabilities as normal methods but they can be only accessed within the block they were declared in.

public int Foo(int someInput)
{
int Bar()
{
Console.WriteLine(“inner function”);
}
return Bar();
}

Tuple Types and literals

  • 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.

Public (int retVal1,string retVal2) ReturnMultipleValues(string input) {……….. }

The syntax (double retVal1,string retVal2) 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<int , 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.

public async Task<(int retVal1,string retVal2)> ReturnMultipleValues(string input) { … }

  • Tuple literals

Tuple values could be created as:

var t = new (double sum, int count) { sum = 0.0, count = 0 };

Record Types

Record Types is concept used for creating a type with only properties. By using that we can embed the constructor declaration with the class declaration.

Class Student(string Name, int Age);

It results in

  • Read-only properties, thus creating it as immutable type.
  • The class will automatically implement Equality implementations like (such as GetHashCode, Equals, operator ==, operator != and so forth).
  • A default implementation of ToString() method

Pattern Matching

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).

Non-Nullable 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.

Immutable Types

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.
Advertisements