Overview of C# 6.0

Overview of C# 6.0

There have been a lot of articles written about the new features of C # but I also want to contribute with a few of my own which I believe are important.
There have been a lot of articles written about the new features of C # but I also want to contribute with a few of my own which I believe are important.

String Interpolation

This is a useful feature when you need to generate rows and bodies in additional data. For example, you need to generate an exception, and add to the message the value of one of the arguments:

1.jpg


Or you need to generate a message when there is an exception:

2.jpg


And even without an exception, it is often useful to get a string representation of the object:

3.jpg


Now you can normally use the StringBuilder, and then use AppendFormat, and then AppendLine, or call the AppendLine, but use string.Format. You can now use AppendLine + String Interpolation:

4.jpg


Sometimes inside the '{}' you can use any expression, including method calls, not just to access the variables / fields / properties.

Improved properties

The second feature is a set of improvements when working with properties. Quite often, I want to get a simple class with a couple of properties and like to push sensible defaults. A good example are some classes configs with the default settings:

5.jpg


It might be a trifle, but the new approach offers significant advantages because it requires less effort, as the output is shorter and easy to understand.

Or we may need a simple data object with the property, for example, such as List <T>.

6.jpg


Or do you want to get a calculated property or properties that pulls a value from another object. In this case you might use:

7.jpg


Yes, here I immediately showed that the expression applies not only to the properties but also to the methods. Now, finally, we can make full get-only auto-props, which can be changed only in the constructor (or initialization in place):

8.jpg


And no additional custom backing fields!

Elvis Operator (?.)

The well-known null-coalescing operator is in third place on my list. The trick is very useful, but is not as applicable as you might think.

9.jpg


Brief and expressive!

However, this feature is useful not only for ignition events. Like it or not, but null-s come and go, and a conditional method call, or obtain a conditional value chain objects are permanent. Now, add the null-coalescing operator (aka '??') and you get a very expressive code:

10.jpg


It is very important not to go too far. If there is a missing value problem, it will be very difficult to say why it was unable to obtain and which object in the chain turned out to be null.

Using static

Now, you can import the static functions in the current scope, and call them without an explicit type name. This is very handy when you have a set of factory methods, but really don’t want to permanently specify the name of the factory:

11.jpg


Here, however, there are both positive and negative points.

On the one hand, the code is more readable because you can focus on the essence of the problem being solved and not clutter up the code type name, which are helper methods. Now you can easily pile internal DSL and obtain a very readable code. On the other hand, the absence of explicit types suggests that the method is somewhere in our class that can be confusing.

There is also an interesting feature when it comes to refactoring. It is easier because you can now make a method outside the class, and the implementation of the current class will remain exactly the same. But if you move to another class method manually, you have to add it yourself using static.

Index Initializer

The new initialization of indexers is useful, but the applicability is completely determined by the type of tasks. In addition this thing has been repeatedly used in the implementation of various factories:

12.jpg


Overall experience

Despite the fact that no large and important new features have yet appeared, the C # 6.0 language seems very pleasant. Many features make the code shorter and more readable, which is very useful. Interpolated strings, auto-props, Elvis operator – are the three things without which will impress you.


Sergey Teplyakov
Expert in .Net, С++ and Application Architecture
Mai ai întrebări?
Conectați-văcu noi