(C# Language) Functional Techniques - Discards

Discards are denoted by an underscore. They can be thought of identifiers that receive an assignment, but the assignment is discarded and not available for use. A discard is most commonly used to indicate that the return type of a function has been ignored intentionally. But there are other less known use-cases of a discard. This tutorial demonstrates them by using various program examples - using a discard to (1) ignore a function return, (2) ignore the return of a tuple, (3) ignore an out parameter of a function, (4) default case of a switch expression and (5) as a forced assignment.
(Rev. 07-Nov-2022)

Categories | About |     |  


Table of Contents (top down ↓)

What is a Discard

A discard can be used to indicate that a developer has purposely ignored the return value of an expression.

Create a C# console project with the latest version of .NET Core. Open the program.cs file. Consider this code where we have discarded the return of the CreateDirectory function. CreateDirectory returns DirectoryInfo for the newly created folder.

We have use a discard to indicate that we have intentionally ignored the return. It is a good programming practice to do so.

// a discard has been used to indicate 
// that we purposely ignored the return value 
// it is a good programming practice to do so 

_ = Directory.CreateDirectory("MyFolder");

Video Explanation (see it happen!)

Please watch the following youtube video:

Another Example

Let's see another use of a discard.

Open the solution explorer and add a class called MyClass.

Let's add a function Print to this class. This function uses a discard to force an assigment of the parameter s. Even though the assignment is going to be discarded at the end of the day, yet it will serve a null test on s. An exception will be thrown if the variable s is null.

public static void Print(String s)
  // a discard can be used to force an assignment 
  // here an exception is thrown if s is null 
  _ = s ?? throw new ArgumentNullException(nameof(s));


Of course, there are other ways of doing the same thing.

Ignoring out Parameters

A discard can also be used to ignore out parameters of a method.

Let's add another function to this class!

public static void Check(int x, out bool IsOdd, out bool isSmall)
  IsOdd = (0 != x % 2);

  isSmall = (x < 10);

We have added a function Check(int, out bool, out bool) to illustrate a concept.

The function has two out parameters. These parameters make a test on an input and determine if it is odd; if it is less than 10. We have added this function just for the purposes of explaining a concept.

Open the program.cs file again. Let's add code to call the function we created just now.

bool isOdd;

// the second out parameter is discarded 
MyClass.Check(10, out isOdd, out _);

Notice that we have used a discard to ignore the second parameter. Without a discard we would have been required to create a boolean solely for satisfying the syntax of an out parameter.

Discards with Tuples

Discards have a good use case with tuples.

Open the MyClass.cs file again. Add a function that returns a tuple of three int numbers. A tuple can be thought of as an anonymous struct. This function returns a tuple of three numbers obtained from sum, product and difference of the parameters x and y.

// a function that returns a tuple 
public static (int, int, int) Calculator(int x, int y)
  int sum = x + y;

  int prod = x * y;

  int diff = x - y;

  return (sum, prod, diff);

Open the Program.cs file again.

Add a call to the Calculator function. Tuple (_, int product, _) is equal to MyClass.Calculator

// discarding returns of a tuple 
(_, int product, _) = MyClass.Calculator(4, 8);

Console.WriteLine($"Product = {product}");

We are interested only in the product of two numbers, so we have used discards to ignore the sum and difference returned by the Calculator function.

This is, therefore, another use of the discards.

Discards with switch Expressions

Discards are also frequently used as a default case of switch expressions.

Open the MyClass.cs file again.

Add a function that returns the name of a day by using a switch expression.

The function uses an expression lambda to return a string.

public static String GetWeekDay(int day) => day switch { 

    1 => "Monday", 

    2 => "Tuesday", 

    _ => "Other or unknown" 


The string is returned from the evaluation of a switch expression. The switch uses two case labels 1 and 2 to return "Monday" and "Tuesday". But it uses a discard for the default case.

This is yet another use of a discard. Thankyou!

This Blog Post/Article "(C# Language) Functional Techniques - Discards" by Parveen is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.