C# 7: First Look

C# is one of the world's most popular programming language, and it continues to evolve in significant ways. This course takes a first look at the newest features now available with C# 7.
Course info
Rating
(150)
Level
Intermediate
Updated
Apr 5, 2017
Duration
45m
Table of contents
Description
Course info
Rating
(150)
Level
Intermediate
Updated
Apr 5, 2017
Duration
45m
Description

C# Programmers want to stay up to date with their programming language, and with good reason. C# 7, the latest release as of 2017, is packed with new and powerful features. In this course, C# 7: First Look, you'll get a first look at C# 7. First, you'll learn about tuples: entities with two or more elements that can be used to return multiple values from methods and for many other use cases as well. Next, you'll get a deconstruction that allows you to break tuples down into local variables. Finally, you'll discover patterns which allow you to match its statements and switch/case statements based on the shape of an object rather than just its value. By the end this course, you’ll know how to use all the new features in C# 7.

About the author
About the author

Jesse Liberty is a Senior Consultant at Wintellect, where he specializes in Xamarin, Azure and Web development. He is a Certified Xamarin Developer, a Xamarin MVP and a Microsoft MVP.

More from the author
Visual Studio for Mac
Beginner
1h 19m
16 Apr 2018
More courses by Jesse Liberty
Section Introduction Transcripts
Section Introduction Transcripts

Course Overview
Hi, my name is Jesse Liberty, and welcome to my course, C# 7: First Look. I'm an independent developer, and I've been programming for three decades. I'm a Microsoft MVP, and a Xamarin MVP, and a Xamarin-certified mobile developer. I've been programming in C# since the pre-release bits. C# 7 is a major release, with many exciting new features. In this course, we're going to take a first look at each of these features, and demonstrate their use. Some of the major topics we'll cover include tuples, which are small entities that contain more than one value. These are great for returning two or more values from a method, but their use goes well beyond that. Deconstruction allows you to break tuples down to their constituent values, and treat these values like variables. And patterns allow you to match is statements, with switch statements, based on the shape of the object, rather than just its value. By the end of this course, you'll know how to use all the new features in C# 7. I hope you'll join me on this journey to learn C# 7's new features with the C# 7: First Look Course at Pluralsight.

Using out Variables
Hi this is Jesse Liberty from Pluralsight and this module is using out variables. We've had out parameters for a long time but they're not terribly fluid. And you have to declare them before you use them. It doesn't make sense to initialize because you're going to overwrite in the calling method. And because of that we can't use var to declare them. To solution to all this is out variables.

Understanding Pattern Matching
Hi, this is Jesse Liberty for Pluralsight and this module is Understanding Patterns. Patterns are elements that can test a value to see if it has a certain shape, patterns come in three flavors, there are constant patterns, there are type patterns and there are also var patterns and we'll see these as we go. Patterns allow you to enhance two existing constructs, these expressions can now have a pattern on the right-hand side, rather than just types and case clauses in switch statements can now match on patterns and not just constants, you can switch on type, you can look for patterns in case clauses and most radical of all, case clauses can now have conditions.

Creating and Deconstructing Tuples
Hi, this is Jesse Liberty from Pluralsight and this module is on Creating and Deconstructing Tuples. Before we define tuples, let's talk about what problem we're trying to solve. We would like to get more than one value returned from a method. Traditionally this was done with out parameters but there are real limitations with out parameters, for example, out parameters tend to be very clunky to use and they can't be used with async methods. An alternative is to use System. Tuple. However, this tends to be very verbose and it requires the allocation of a tuple object. That allocation is a bit excessive just to get a few values out of a method. We could use an anonymous type, however, once again, we're looking at a high performance overhead and most important, we lose static type checking. So, tuples can be a return type and they can be a literal. Tuples can have two values, three, four, in fact, some people call them twoples but I find that misleading a bit because of course there can be more than two. Each element in a tuple can be accessed with dot notation and they are automatically named Item1, Item2 and so forth. You can even name the return tuple parts as you see here with firstName, middleInitial and lastName. Between the parentheses is our tuple with three parts. There are a few things to know about tuples. For one, you can convert one tuple into another. Second, tuples are value types, not reference types. And thirdly, tuple elements are public and mutable and should be thought of as fields.

Creating Local Functions
Hi, this is Jesse Liberty from Pluralsight. And this module is Creating Local Functions. A local function is created within another function and fully encapsulated within that function. This allows helper methods to be created inside the function that they help. It's worth noting that parameters and local variables from the enclosing scope are visible within the local function.

Language Enhancements
Hi, this is Jesse Liberty from Pluralsight, and this module is Utilizing Language Enhancements. C# 7 offers a very convenient way to break up literal values. It also offers a new feature, Return by Reference, and it allows you to throw exceptions as expressions.

Summary
Hi this is Jesse Liberty from Pluralsight. And this module summarizes what we've seen in the rest of this course. We took a look at out variables and saw how they can provide a cleaner use for out parameters without the need to predeclare them. We looked at the very powerful new feature of Tuples which allow you to create multiple values in a single entity, which can be literals or passed back from methods. They can also be used as indexes into dictionaries. You can deconstruct your Tuples into their constituent parts as variables. Or you can even deconstruct them into existing variables. We looked at patterns, which allow you to match against the shape of an object and this affects the is statement as well as switch statements, where the case can match against a pattern. We also saw how switch statements can now have conditionals. Local functions are functions within functions and we saw that this can provide very elegant solutions to otherwise thorny problems. We saw that literals have been enhanced to allow you to use underscores to make them more readable and we saw that you can now return non-reference variables by reference and we saw a way to use this as a very elegant solution to a problem that otherwise would be much more cumbersome and finally we saw that you can throw exceptions as expressions, giving you more flexibility and when you throw exceptions.