Skip to content

C# Feature - Discards

Discards use _ to explicitly ignore values you do not need, which keeps intent clear and avoids placeholder variables.

C# 7.0 (2017)

int parsedValue;
bool ok = int.TryParse(text, out parsedValue);
bool ok = int.TryParse(text, out _);
var (_, month, _) = DateTime.UtcNow;
if (value is not null and not "")
{
Console.WriteLine(month);
}
  • Use a real variable when the value may become meaningful later in the method.
  • A discard is not a reusable variable; each _ is just an ignored target.
  • C# 7 introduced discards for deconstruction, pattern matching, and out arguments.