was successfully added to your cart.

Top 5 New C# 7.0 Features in Visual Studio 2017

The latest version of Visual Studio came out recently, and with it came a bunch of new C# 7.0 features. Here are my top 5 new features:

The Code

Inline Out Variables

These variables allow you to declare an out variable inline. Simple, right? Actually, it is so simple it is brilliant. Why declare a variable in a separate line when the only thing you are going to do with that variable is populate it with the results of the function? Here is the demo code I showed in the video (just the new code):

Console.Write("What is your age: ");
string ageText = Console.ReadLine();
// Out variables declared inline
bool isValidAge = int.TryParse(ageText, out int age);

Console.WriteLine($"Your age is { age }.");

One little change and you save a line of code (and some confusion). That’s a big deal in my book.

Pattern Matching

This is one that you are going to use a lot. Pattern matching allows you to evaluate what the object is and then put it into a variable if it was the type you were expecting. In our example, we got really specific. We decided to evaluate an object to see if it was a number or a string that could be parsed to be a number. Pattern matching doesn’t have to be that specific, but the power of pattern matching really comes through in that instance.

Here is the code from the video:

string ageFromConsole = "21";
int ageFromDatabase = 84;

object ageVal = ageFromDatabase;

if (ageVal is int age || (ageVal is string ageText && int.TryParse(ageText, out age)))
 Console.WriteLine($"Your age is { age }.");
 Console.WriteLine("We do not know your age.");

Note that you can change which variable’s value you put into the ageVal variable (either ageFromConsole or ageFromDatabase) to test out how pattern matching works.

Powerful Switch Statements

For a long time, switch statements have been avoided. They aren’t nearly as functional as an if/elseif/else set of statements. Now switch is finally getting some attention. We can now do pattern matching inline in a statement and we can filter our cases using a when statement.

Here is the relevant code we used in the video:

foreach (var p in people)
 switch (p)
 case Employee e when (e.IsManager == false):
   Console.WriteLine($"{e.FirstName} is a good employee.");
 case Employee e when (e.IsManager):
   Console.WriteLine($"{e.FirstName} runs this company.");
 case Customer c when (c.TotalDollarsSpent > 1000):
   Console.WriteLine($"{c.FirstName} is a loyal customer.");
 case Customer c:
   Console.WriteLine($"{c.FirstName} needs to spend more money.");

Are you going to stop using if/elseif/else statements completely? Nope, but this does elevate switch statements to a much larger seat at the table.

Throw in Exceptions

This one might be a feature that you won’t use that much, but when you need it, it will be great to have. You can throw an exception right inline with an evaluator. In our case, we used the null coalescing operator (??) to say we either found a record or we throw an exception.

Here is the relevant code:

Employee ceo = people.Where(x => x.IsManager).FirstOrDefault() ?? throw new Exception("There was a problem finding a manager.");

I can think of code I have in production right now that I want to change to take advantage of this feature.

Improved Tuples

This one is my favorite. Tuples are finally a first-class citizen in C# 7.0. A tuple is a mechanism by which we can pass around two or more values¬†without declaring a structure or class. In this new version, Tuples can be returned from a function. That means that a function’s return type can be multiple named values.

Note: As of this article’s publication, you need to add the System.ValueTuple NuGet package in order to get tuples to properly work. This should be folded directly into C# 7.0 sometime in the near future.

Here is the code for a method that has a tuple return type:

public static (string firstName, string lastName, bool itWorked) SplitName(string fullName)
 string[] vals = fullName.Split(' ');

return (vals[0], vals[1], true);

And now to use it, you could have code like this:

var name = SplitName("Tim Corey");
Console.WriteLine($"The first name is { name.firstName } and the last name is { name.lastName }.");

Pretty cool, right? I can’t tell you how many times this would have reduced the complexity of my code. Instead of doing work-arounds, extra models, or out variables, I can just return a tuple.


The new C# 7.0 features are excellent. I love the update. The thing I appreciate the most, though, is the focus on coding efficiency. Did you notice that? My top five all have something to do with making the coding experience more powerful and yet more simple. Adding power while reducing complexity is the mark of a great programmer. Sometimes, in the desire to give us more, Microsoft has made things more complex. Not with this release. We still get more features, but the are focused on allowing us to do our job better, faster, and cleaner. I love it. What do you think? Leave me a comment and let me know.

Join the discussion One Comment

Leave a Reply