Overview of C# 6.0
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:
Or you need to generate a message when there is an exception:
And even without an exception, it is often useful to get a string representation of the object:
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:
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:
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>.
Or do you want to get a calculated property or properties that pulls a value from another object. In this case you might use:
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):
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.
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:
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:
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:
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