C# Feature - Discards
Overview
Section titled “Overview”Discards use _ to explicitly ignore values you do not need, which keeps intent clear and avoids placeholder variables.
Introduced In
Section titled “Introduced In”C# 7.0 (2017)
Before
Section titled “Before”int parsedValue;bool ok = int.TryParse(text, out parsedValue);bool ok = int.TryParse(text, out _);Annotated Example
Section titled “Annotated Example”var (_, month, _) = DateTime.UtcNow;
if (value is not null and not ""){ Console.WriteLine(month);}Gotchas & Best Practices
Section titled “Gotchas & Best Practices”- 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.
Related Features
Section titled “Related Features”Version Notes
Section titled “Version Notes”- C# 7 introduced discards for deconstruction, pattern matching, and
outarguments.