New features in C# 7

C# 7 focuses on better developer productivity and enhanced performance

csharp 7

C# 7 adds a lot of new features with special focus on simplicity in coding and enhanced performance. It provides a lot of focus on data consumption, code simplification, and performance, and it's designed to eliminate the pain of having to write extra code in more ways than one.

Here's a quick glance at what's new and enhanced in C# 7.

  • Support for Local functions
  • Enhanced support for Tuple Types
  • Record Types
  • Pattern matching
  • Non Nullable reference types
  • Immutable types
  • Better support for out variables

Better support for out variables

The ability to declare an out variable right at the point of usage is a great new feature in C# 7. Here is an example code snippet that illustrates this.

public void Save(Product p)


    p.SaveData(out int x);

    //Usual code


A good place to use out variable is inside your try block. Just use a Boolean out variable where a return type of true indicates that the operation is a success, false otherwise.

Pattern matching

C# 7 provides excellent support for pattern matching. You can use pattern matching on any data type, even on custom data types as well. Moreover, you can take advantage of pattern matching to extract values from an expression. Here's a code snippet that illustrates pattern matching at work!

object obj = "This is a demonstration of pattern matching in C# 7";

if (obj is string str)




Support for Local Functions

You might often need a helper function that needs to be used only once -- maybe in just one method. You can now declare such functions inside another function. Such functions are known as local functions. In essence, the support for local functions enables you to declare methods inside a block scope. Albeit the fact that this was possible using Func and Action types with anonymous methods in the earlier versions of the C# language, there were few challenges. They did not support generics, params and ref and out parameters.

Better support for Tuples

A Tuple is a temporary grouping of values. It is similar to a POCO class but one that is created on the fly. The main advantage of using a Tuple is enabling a method to return multiple values. You may want to use a tuple to represent a set of heterogeneous data and provide an easy way to access that data. Tuples are nothing new and have been around for quite some time now. You have support for tuples in the F# and Python programming languages. You can take advantage of a tuple to store an ordered, finite sequence of immutable, finite sequence of homogeneous or heterogeneous data of fixed sizes.

You now have support for Tuple literals and Tuple deconstruction as well. In C# 7, a tuple is considered a value type. Hence it is a mutable type and much more efficient as far as performance is concerned.

Non-nullable reference types

Nullable value types were first introduced in C# 2.0. Non-nullable reference type is exactly the opposite of this feature. Essentially, a non-nullable reference type is used to denote a reference type that is guaranteed not to be null. Here's how a non-nullable reference type is declared in C# 7:

string! str; //This is a non-nullable reference type

Better support for Immutable objects

An immutable object is one the state of which cannot be altered once it is created. This makes an immutable object thread safe. Suppose you create a class that contains a property that has a getter but no setter. Yes apparently, it seems that an instance of the class is immutable. However, at a later point in time, if someone adds a setter for the same property, the immutability is lost, isn't it?

Here’s exactly where the better support for immutable types comes in to the rescue. With C# 7, you can create a new instance based on another instance. Here's how this can be achieved.

var firstObject = new Employee(101);

var secondObject = firstObject with {EmployeeId = 102};

Record types

Record types enable you to create a type using only properties. In essence, a record type is a data type that comprises of properties only. The following code snippet illustrates how a record type can be declared.

 class Rectangle(int Height, int Breadth);

Copyright © 2016 IDG Communications, Inc.