With the release of Visual Studio 2015 last week, I thought it would be a good time to inspect the new features that come with the newest version of C#. Although C# 6.0 is certainly not a major release from a developer’s perspective, there are some items that every developer will use in the coming years. In this post, I’ll show some examples on how to use these new features.
The Visual Studio 2015 (compatible with at least VS2013) solution with code samples can be downloaded here. The following topics will be covered in this post:
- nameof expressions
- string interpolation
- null conditional operators
- index initializers
- static import
- Getter-only auto-properties
- expression-bodied function members
- exception filters
1. NameOf Expressions
Suppose you have the following class property and you would like to retrieve the name of the property as a string.
Before the existence of the new..
keyword, you would have to write some clunky code to achieve this. In C# 6, you could use the following syntax:
2. String Interpolation
Although the string.Format functionality provides quite some flexibility when it comes to creating dynamic text, you’d still have to use the static Format method on the string class. By adding a dollar sign before a literal or string variable in C#6, you can insert your placeholders directly in your text. The following code sample shows the difference between old and new:
3. Null Conditional Operators
This is a very useful new feature. A lot of code in .NET is dedicated to null reference checking. Imagine now that you could skip most of that with one simple keyword! Now this is possible, and it’s quite easy to use:
If the randomList object is null, the code after the ? symbol won’t be executed. Because this code won’t be executed, the filteredList variable will be equal to the default value, which is null in this case. This means you’ll have to continue to check for null references throughout your code.
4. Index Initializers
Setting properties during the initializing process has been out for some time now, but for some reason it wasn’t possible for dictionaries. In C#6, they have closed this gap with the same syntax:
5. Static Import
This is a feature that I do not support, as it will add ambiguity to the application. Importing static classes can now be done directly instead of referencing it inside your methods and properties. Following code shows what needs to be done to achieve this:
I quite like the old way of writing this code. It is very clear that the ForEach method on the Parallel class is called and not any other method.
6. Auto-property initializers
This new feature can replace some code that you would otherwise write in a default constructor. The following syntax is quite comprehensive:
It is even possible to omit the setter, and still this code would return “Test”.
7. Expression-bodied function members
You can now use inline code using the lambda syntax to give some body to your members and methods. This can come in handy for simpler statements. For complex code, I’d suggest you still use separate statement blocks (i.e. the traditional way):
8. Exception filters
The final topic that I’ll discuss are exception filters. You can add conditions to exception blocks. The key here is the when keyword and the code that follows, which must return a boolean.
If the boolean expression returns true, the code between the curly braces will be executed. If it returns false, the code block will not run and it will be like as if there wasn’t an exception block. This means the exception will bubble up or will continue to the next exception in the call stack.