Introduction
Microsoft added a lot of new syntax goodies in their latest release of C# that will make you more productive, namely:
- Index from the end operatorĀ ^
- Range operatorĀ ..
- Null coalescing operatorĀ ??=
- Using variables
- Verbatim string with interpolationĀ $@Ā @$
Index from the end operatorĀ ^
^
^
Ā specifies the index from the end of a sequence. It simplifies the calculation required to get the correct index from the start of the array. Now, you can directly specify the index from the end, which is easier to understand and shorter.e.g., Get the second last item of an array
var array = new[] {1,2,3,4,5};
// Before
array[array.Length - 2]; // 4
// Now this is also allowed
array[^2]; // 4
For more details, please take a look at my article onĀ indices and ranges.
Range operatorĀ ..
..
Selecting sub-arrays in C# used to imply a lot of boilerplate code. WithĀ .., there's now a super simple way to do it.
e.g., Get a subarray
var array = new[] {1,2,3,4,5};
// Before
var newArray = new List<int>();
for (var i = 1; i < 3; i++)
{
newArray.Add(array[i]);
}
// Now this is also allowed
var newArray = array[1..3]; // 2,3
For more details, please take a look at my article onĀ indices and ranges.
Null coalescing operatorĀ ??=
??=
??=
Ā is a helpful little operator that allows you to assign a default value in case of a null.// Before
var possibleNullValue = possibleNullValue ?? "default value";
// Now this is also allowed
var possibleNullValue ??= "default value";
Using variables
using
Ā statements are handy to make sure to clean up disposable resources at the end of their used scope. However, when you have to chain multipleĀ usingĀ
statements, your code needs to be indented, and it can get ugly pretty fast. It's a good practice to make sure that disposable resources don't outlive the scope of the method where they are instantiated. So why not just dispose of them at the end of the method's scope? It's what Microsoft did with the newĀ
using var
. A subtle side effect is that the operator also gets rid of all the unnecessary parentheses and curly braces. Hurray!// Before
using (var stream = new FileStream("", FileMode.Open))
{
using (var sr = new StreamReader(stream))
{
...
}
}
// Now this is also allowed
using var stream = new FileStream("", FileMode.Open);
using var sr = new StreamReader(stream);
...
Verbatim string with interpolationĀ $@
Ā @$
$@
@$
Verbatim stringsĀ @Ā andĀ string interpolationĀ $Ā are two useful concepts to build string literals. However, if you wanted to use both on the same string literal, you had to specify them in the correct order; otherwise, you were getting an error from the compiler.
Not anymore! You can now specify them in any order, and the compiler will figure it out for you.
// Before
$@"This -> {nameof(this)}";
// Now this is also allowed
@$"This -> {nameof(this)}";
Closing word
I hope you enjoyed learning about these tips and tricks. If you are curious about other new features introduced with C# 8.0.
All code samples are available onĀ github
Previously published at https://blog.miguelbernard.com/5-tips-to-improve-your-productivity-in-c-8-0/