Skip to content

Lambda discard parameters C# 9.0correctnessreadability

Signify no interest in a parameter.

C# 7.0 added the ability for methods to discard parameters, and C# 9.0 extends this to lambda and anonymous methods so you can signal that you are not interested in a parameter which is required by the signature.

Code analysis tools stumble trying to decide if the unused parameter was intentional or should be removed.

Discard parameters allows you to name any such parameter _ to signal that this omission is intentional.

Code

C#
Action<string, int> logger = (string message, int indent)
    => Log(new string(' ', indent) + message);

if (disableLogging)
    logger = (_, _) => { };

logger("OK", 0);
C#
Action<string, int> logger = (string message, int indent)
    => Log(new string(' ', indent) + message);

if (disableLogging)
    logger = (message, indent) => { };

logger("OK", 0);

More information