The releases of Visual Studio 2017 and Visual Studio for Mac introduced two spectacular new IDEs to develop mobile applications, cloud workloads, and introduced us to a world of powerful new C# features with the release of C# 7. I recently blogged about my favorite new feature, expression bodied members, and even recorded an entire podcast about how awesome C# 7 is. In this blog post, I’ll show you how to get started with C# 7 and some features that you can take advantage of right away in your mobile apps.

Tuples!

When you want to return multiple values from a function, Tuples are the way to go. C# 7 provides rich syntax and functionality that make Tuples a first class citizen with the introduction of the System.ValueTuple NuGet package. After installing the NuGet, we’re ready to start using Tuples as lightweight data structures with full IntelliSense.

Tuple Declaration

We can now create Tuples in several ways:

As you can see, this automatically creates a Tuple with members of Item1 and Item2, but we can do better!

Or…

Returning Tuples

Now that we know how to declare tuples, we can use them in a method:

Then we can simply call the method:

Or…

You can also use full async/await support with Tasks!

More Expression-Bodied Members

I can’t talk about C# 7 without mentioning my favorite feature, expression-bodied members, which were introduced in C# 6 and greatly enhanced in C# 7. Here’s how they improved our code in C# 6, using our good friend OnPropertyChanged as an example.

Now, with expression-bodied members we can write this:

Amazing! However, in C# 7 things get even better. Take a look at the standard get and set accessors that I use all the time:

Look at all of those { and }, well no more! Here is some C# 7 for you:

They can now also be applied to constructors, finalizers, and indexers!

Pattern Matching

I love expression-bodied members, but I have to admit Pattern Matching is an absolutely delightful feature when using switch and is expressions. At its core, it enables us to inspect an object and determine if it satisfies a specific pattern that we’re expecting.

The most simple example is when you receive an Object type and need to figure out what it is. Let’s say we have a base class of Animal with several derived classes from it for specific animal types:

Now, we can simplify this by combining pattern matching and the code expression and casting the item in line:

Pattern Matching doesn’t stop there, as we can use it in switch cases as well:

Awesome out variables

This one is pretty straight-forward, but also extremely powerful. When using a method that has an out variable, you always have to declare it ahead of time, as such:

Not anymore! Now we can simply declare the variable inline and use it anywhere!

Learn More

These are just a few of the new features that can be found in C# 7, which you can start using today in Visual Studio 2017 and Visual Studio for Mac. Be sure to read through the entire C# 7 documentation to learn more about all of the other exciting new features available to you right now.