Within this tutorial, you will learn how to master discards within C#. The discard variable was first released in C# 7 in 2017. When reading code, you can spot a discard whenever you bump into an underscore _
being used rather than an explicit variable. When a discard is used in code, any values that are assigned to the discard are ignored.
When I first started learning about discards, I did not think I would use them much, however, once you master their purpose they can be a very handy tool to have in your toolbox.
There are at least five areas in your code where discards can make your code more readable. If you are interested to learn more about these scenarios and where you can use discards within your codebase, read on 🔥🔥🔥
Like a lot of other developers, when first learning about discards you might think, why bother? A discard variable ignores any values that are assigned to it. Instead of using a discard, why not just use a well-named variable and then do nothing with it? The answer lies within communication...
When you explicitly use a discard somewhere in your codebase, you are conveying your intent. By using a discard, you are telling future readers of your code that you do not care about the value assigned to it. When scanning any code and you bump into a discard, you know that the values being assigned to it are not important and can be ignored (assuming the code has no bugs!). In essence, the discards give you a way to specify in the code whether the return value is important, or not!
As a side benefit, a discard variable saves on memory. As any values assigned to it are ignored, no new areas in memory need to be assigned. Let us be honest, with C# and automatic garbage collection, I do not think this small saving will create any meaningful performance impact on your codebase, however, it is better to follow good practices when possible.
When you don't explicitly capture the return value within your code and ignore it, you create ambiguity. Did you forget about the return value, was this slip on purpose, or, an accident? When you use a discard you clearly say, you do not care!
When you think of a discard as a tool to make your code more readable, the next question is where can you use them. Technically, you can use a discard anywhere you use a normal variable, however, in most scenarios it only makes sense to use discards in certain situations. This raises the question, where are those areas? Let us now look at five different areas where we can see this in action:
List pattern matching
Arguably the most useful place where you can use the discard variable is within any code where you want to perform some pattern matching on an enumerable list. Using the discard variable here will allow you to define the pattern that you want to match by
In this example, the pattern will match with a list that has the number 3 in the 3rd position. The three discard variables are used to ignore everything within the pattern.
Guards and null checking
One of the most common uses that I have seen discards being used in code, is as a replacement for a guard. A guard is simply a way to check if a passed-in argument is populated. A simple example of a guard is shown below:
A guard clause can be written without the need for an if
by using a discard instead. By combining a guard and the null coalescing operator, you can refactor that same code onto a single line, like this:
Without assigning the operation to the discard variation this code would not compile nicely and a warning would be thrown. You could also not swap the discard with another variable as the return types are different. In this instance, the discard allows the use of operations and not worry about assignments 😎
Asynchronous methods
Another handy area to use a discard is when you need to call an asynchronous method and you do not care about the response. Imagine that you want to run an A/B test. To determine the winner you need to track when someone has visited a page. When the user triggers the action, you want to fire off an event to an API. You do not care about anything that may be returned from the call, you just want to send it. Instead of ignoring the response, you can assign it to a discard and make the code intentional.
In C#, you can spot an asynchronous method as it will be used in conjunction with the async
operator. If you want to fire off a request that is marked as async and you do not care about the result, you could write some code like this:
Without using the discard, you would get a compiler error, like this:
TryParse() and out
Let us say you need to call a TryParse()
. You do not need to use the out
parameter, all you care about is the operation.
Methods that return multiple-data points
switch statements
When creating a switch statement, you can assign the default condition to a discard. Instead of using the default
keyword, it is also valid to use discard in its place:
By now, I am hoping that you can see the value in the discard variable and that you have a better understanding of where you can use them. Discards are a handy way to make your code more readable, DO NOT discard this knowledge 🤪🤪🤪
Happy Coding 🤘