DEV Community

Cover image for 5 tips to improve your productivity in C# 8.0
Miguel Bernard
Miguel Bernard

Posted on • Originally published at blog.miguelbernard.com

5 tips to improve your productivity in C# 8.0

Also in this series

  1. C# 8.0 Nullable Reference types are here!
  2. Pattern matching in C#
  3. Asynchronous streams
  4. Indices and ranges
  5. Default interface methods
  6. 5 tips to improve your productivity in C# 8.0

All code samples are available on github

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 ^

^ 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, look at my other posts in this series πŸ‘‡.

Also in this series

  1. C# 8.0 Nullable Reference types are here!
  2. Pattern matching in C#
  3. Asynchronous streams
  4. Indices and ranges
  5. Default interface methods
  6. 5 tips to improve your productivity in C# 8.0

All code samples are available on github

Top comments (0)