C# 7.0 – Discards


Sometimes there is a need to ignore a result of a method. It’s straightforward if that’s a return value – just don’t assign it. But what should be done in other cases?

Note: You can view all code examples on GitHub. My environment: Visual Studio 2017 and .NET Framework 4.6.1.

        // Let's have a method that returns a value
        private int DoSomethingAndReturnValue() => 24;

        private void Example1()
            // Get the value
            var myValue = DoSomethingAndReturnValue();

            // Ignore the value

Out parameter discards

Ignoring an out variable value before C# 7.0 was hard. The assignment was needed.

        private void DoSomethingAndGiveValue(out int value) => value = 42;

        private void Example2()
            int ignoredVariable;
            DoSomethingAndGiveValue(out ignoredVariable);

New C# 7.0 feature, out variables, made a bit nicer.

        private void Example3()
            DoSomethingAndGiveValue(out var ignoredVariable);

But there is still a new variable introduced and assigned. Fortunately, there is one more new feature that could help – discards.

        private void Example4()
            DoSomethingAndGiveValue(out int _);

The underscore character (_)  is used to indicate that the parameter should be ignored. It’s like it is the write-only parameter.

It’s also allowed to discard a variable type.

        private void Example5()
            DoSomethingAndGiveValue(out _);

            // The following won't compile as discards is not a variable that one can read from.
            // System.Console.WriteLine("Print discard: " + _);

Discards could be used to ignore more parameters at once.

        private void GetManyValues(out int first, out int second, out int third, out int forth) =>
            first = second = third = forth = 13;

        private void Example6()
            GetManyValues(out _, out int thisOneWeNeed, out _, out _);

The simplest discard

Discard is not limited to only out parameters. It can be used to ignore any variable.

        private void Example7()
            // Here is the simplest discard example
            _ = 1 + 2;

            // The following still won't work
            // System.Console.WriteLine("Try print discard again: " + _);

Discards with deconstruction

Deconstruction is one more C# 7.0 feature. It allows to split tuples (and other objects) to individual values. But what if not all values are needed? Yes, discards can help as well.

        private void Example8()
            var tuple = (1, 1, 2, 3, 5, 8);
            (_, _, _, _, var fifth, _) = tuple;

Discards with pattern matching

Discards are only allowed in patterns in the is statement for the time being. But this should be extended in future versions of C#.

        private void Example9()
            object x = 13;
            if (x is var _)
                // Still can't use discard as variable :)
                // System.Console.WriteLine("Print: " + _);



Leave a comment

Your email address will not be published. Required fields are marked *