5 tips to improve your productivity in C# 8.0
Tags: dotnet, csharp, aspnet
Published: 2020-02-25 00:42:02

<!-- omit in toc -->
## Also in this [series](https://blog.miguelbernard.com/csharp-8-0-new-features/)

1. [C# 8.0 Nullable Reference types are here!](https://blog.miguelbernard.com/c-8-0-nullable-reference-types-are-here/)
2. [Pattern matching in C#](https://blog.miguelbernard.com/pattern-matching-in-csharp/)
3. [Asynchronous streams](https://blog.miguelbernard.com/asynchronous-streams/)
4. [Indices and ranges](https://blog.miguelbernard.com/c-8-0-indices-and-ranges)
5. [Default interface methods](https://blog.miguelbernard.com/what-are-default-interface-methods-in-c-8-0)
6. [5 tips to improve your productivity in C# 8.0](https://blog.miguelbernard.com/5-tips-to-improve-your-productivity-in-c-8-0)

> All code samples are available on [github](https://github.com/mbernard/csharp8sample)

<!-- omit in toc -->
# 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 `^`](#indexfromtheendoperator)
- [Range operator `..`](#rangeoperator)
- [Null coalescing operator `??=`](#nullcoalescingoperator)
- [Using variables](#usingvariables)
- [Verbatim string with interpolation `$@` `@$`](#verbatimstringwithinterpolation)

## 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

```csharp
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](https://blog.miguelbernard.com/c-8-0-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

```csharp
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](https://blog.miguelbernard.com/c-8-0-indices-and-ranges/).

## Null coalescing operator `??=`

`??=` is a helpful little operator that allows you to assign a default value in case of a null.

```csharp
// 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!

```csharp
// 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 `@`](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/tokens/verbatim) and [string interpolation `$`](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/tokens/interpolated) 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.

```csharp
// Before
$@"This -> {nameof(this)}";

// Now this is also allowed
@$"This -> {nameof(this)}";
```

<!-- omit in toc -->
## 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 👇.

<!-- omit in toc -->
## Also in this [series](https://blog.miguelbernard.com/csharp-8-0-new-features/)

1. [C# 8.0 Nullable Reference types are here!](https://blog.miguelbernard.com/c-8-0-nullable-reference-types-are-here/)
2. [Pattern matching in C#](https://blog.miguelbernard.com/pattern-matching-in-csharp/)
3. [Asynchronous streams](https://blog.miguelbernard.com/asynchronous-streams/)
4. [Indices and ranges](https://blog.miguelbernard.com/c-8-0-indices-and-ranges)
5. [Default interface methods](https://blog.miguelbernard.com/what-are-default-interface-methods-in-c-8-0)
6. [5 tips to improve your productivity in C# 8.0](https://blog.miguelbernard.com/5-tips-to-improve-your-productivity-in-c-8-0)

> All code samples are available on [github](https://github.com/mbernard/csharp8sample)