New Features in C# 7

In this article, we’ll look at some of the new language features of C# 7 that will help us write cleaner, less buggy code in a shorter time.

C# 7

Binary literals

In C# 6 we can currently represent integers in decimal or hexadecimal formats.

int d = 42;  
int h = 0x2A;

In C# 7, we also have binary literals.

int b = 0b101010; 

No more folding binary values into hexadecimal.

Literal separators

I’m not sure if it’s only me, but literal separators are going to make my life a little less annoying.

I’m a data scientist—so there’s a lot of numbers. And, I’m forever placing my cursor on them to count the digits.

int n = 1000000000;

Is that a billion, one hundred million, ten billion? I’ve no idea.

What about this?

int n = 1_000_000_000;

Yep. Thought so. Much easier, right?

Underscore (_) can be used as a digit separator inside numeric literals. It has no impact on the semantics of the number—just the presentation.

It also works with floating point literals.

decimal d = 1_000_000_000.999_999m;

Better tuples

When you want to return more than one value from a function you can do one the following in C# 6:

  • use an out parameter
  • return an object
  • use a tuple through System.Tuple

Tuples are a neat solution, but the C# implementation has always been clunky. C# 7 changes this. Tuples are now much nicer.

First, install the System.ValueTuple package from NuGet.

Install-Package "System.ValueTuple"

Then we can write code like this

private static (string description, int quantity) GetStock(int productId)  
{
   return (description: “Widget”, quantity: 10);  
}

var stock = GetStock(1);  
Console.WriteLine($”We have {stock.quantity} units of {stock.description}”);

Out variables

The improvements to tuples are likely to reduce dependence on out parameters—which is a good thing, as they are ugly.

int n;  
if (int.TryParse("42", out n))  
{
   Console.WriteLine(n);  
}

In C# 7 we can remove that “orphaned” declaration.

if (int.TryParse("42", out int n))  
{
   Console.WriteLine(n);  
}

Cleaner.

Local functions

Sometimes we only need to use a function as a helper for another function. Adding the helper as a separate private function elevates its status beyond its role.

In C# 7 we can define a function as local to another function.

public static void DisplayStock(string description, int quantity)  
{
   Console.WriteLine($"We have {quantity} {Pluralize(description, quantity)}");

   string Pluralize(string singularNoun, int count)  
   {
       return $"{singularNoun}{(count == 1 ? string.Empty : "s")}";  
   }
}

Now the Pluralize function is formally a helper for DisplayStock.

Pattern matching

One of the most exciting new language features in C# is pattern matching. Pattern matching in languages such as F# is incredibly powerful and we now have some of that power in C#. Apparently, we are going to see the pattern matching features extended in the future.

Currently, one of the most useful things we can do with pattern matching in C# 7 is switch based on type. So we can have code such as:

switch (product)  
{
   case Book b:  
        Console.WriteLine(b.Author);  
        break;

  case Album a:  
        Console.WriteLine(a.Artist);  
        break;

  case Game g:  
        Console.WriteLine(g.Developer);  
        break;  
}

There is a host of other new features in C# 7. This article has covered some of the more accessible ones—quick wins. I encourage you to start using them.

Learning Tree

Whether you are new to .NET development, or experienced, but would like to learn more, Learning Tree has you covered. Check out the courses on our .NET/Visual Studio curriculum.

Type to search blog.learningtree.com

Do you mean "" ?

Sorry, no results were found for your query.

Please check your spelling and try your search again.