By Ognjen Babić | Software Developer 28.12.2016.
What is new in C# 7
C# language is changing a lot and improving vastly on regular basis. As a result (one among many), we got a lot of syntactic sugar that we can use and not worry about low level stuff that happens behind the scene. Of course, to remember these changes you will need at least to try them out in a test project or something like that. And do it quickly as Microsoft started pumping out their updates as fast as possible. With that rate, maybe they will catch up with Call of Duty and release them on regular one year basis (smile). In next lines, I will give you an insight on what are some new interesting features of C# 7 and how to use them.
This new feature should not be confused with destructors. They still exist and are used for disposing purposes. On the other hand, deconstructors are used to split up object into some usable parts. Think of it this way, when you are creating object you can pass some parameters into its constructor and use them further on. If for some reason, you need initial parameters back, you can provide them via deconstructor. And with new tuple syntax it is really easy to do it. More on the tuple part in next section. For now, don’t freak out when on Figure 2 you see some variables in parentheses.
We define base user class with two properties, constructor and deconstructor. On second image you can see how it can be used and new tuple syntax.Figure 1 User class Figure 2 Usage of deconstructor
For those not familiar with tuples, fear not, here is a short intro. Tuples represent light objects in which we can put any kind on data. We use them when we want to transfer some object type variables that can be used anywhere in code. They are special because we don’t have to create new class, as they are already implemented and we can use them freely. Until C# 7, you could use them as any regular object type (Figure 3).Figure 3 Creating Tuple object
Now you can still use them this way, but also you can use new syntax and pair them with deconstructors too. You can define all fields of the tuple in declaration itself, prior to the declaration or just use the var keyword. You can also create custom instances of tuples by providing values in declaration. See them in action on Figure 4.Figure 4 Usages of new tuple syntax
You did this a lot of times, usually because it just can’t be avoided. Whenever you reach a point to check for some type you end up writing a bunch of boilerplate code. This generally happens when you can’t enforce polymorphism because of already bad code design, source code being not available or cannot be changed, etc. In these cases, you would use casting and catching exceptions, as operator and checking for null or is operator and then casting to the correct type. Any of the cases create lots of code for a relatively simple task to do, especially when done multiple times. Now, with type casting pattern matching we can implement checking for types in if or switch statements way cleaner than ever before. On next figures if and switch statements will be shown containing new type casting syntax. In example below we want to make sure if user is premium or not and add subscription to it. Besides casting, we can add more conditions that need to be satisfied in order to proceed further in if branch.Figure 5 Type casting using if statements Figure 6 Type casting using switch statement
Local functions are new functionalities with accent on more scope restriction. They can be useful when you need to place some logic in separate mini method that can be used only within a single method. Doing this, you limit usage of that mini method to only the parent method (in which it is created), so it can’t mistakenly be used anywhere else in the class. You cannot specify accessibility modifier on local functions and they do not support overloading. In example below method for detecting square brackets in string is used as a local function. One thing to note, it does not matter where in method you define local function, it can be used before or after declaration.Figure 7 Local function
This was highly request feature as many developers complained about declaration of out variables before usage. The most obvious case where this is used is with converting some values via TryParse methods. Out keyword assures you that inside the method value for your variable will be set, so if you did not use this variable before, this is the best place to declare it. Two lines of code converted into one, Yay. On next image, you can see inline declaration of out variable.Figure 8 Inline out variable declaration
Underscore in numbers
This is a small change which is focused on readability. When working with large numbers you can use literal underscore (‘_’) to format parts of numbers (marking thousands, millions, etc.) or when specifying them in binary format. Some of the examples you can see on figure 9.Figure 9 Using underscore in numbers
Expression bodied members
C# 6 brought us expression body methods and property getters which reduce to unnecessary code lines. Following this trend, C# 7 introduced even expression like syntax. Now we can use it for property setters, constructors, finalizers, etc. On figure 10 you can see them in action.Figure 10 Expression body syntax
Throwing exceptions is often used when checking for an argument in a method. We can now simplify that null check process (or check for some other value), again, in single line. To accomplish this, we can use conditional operator(?) or null-coalescing operator(??). Example is shown on next figure.Figure 11 Throwing exceptions
By Ognjen Babić | Software Developer 28.12.2016.Follow him on LinkedIn