Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

Discards are write-only, unassigned dummy variables in C# represented by the underscore (_) token. They instruct the compiler that a value is intentionally ignored, which prevents the allocation of memory for a named variable and suppresses compiler warnings regarding unused assignments. Because discards do not possess a value and cannot be read, multiple discards can be utilized within the same scope without triggering identifier collision errors. The compiler infers the type of the discarded value contextually, but the discard itself is untyped.

Syntax and Mechanics

The discard pattern is integrated into several language constructs where a value must be syntactically provided or received, but the value itself is not required by the execution logic. Tuple and Object Deconstruction When deconstructing a tuple or an object, discards can be used to ignore specific elements while capturing others.
// Ignoring the first and third elements of a tuple
var (_, activeUser, _) = GetUserStats();
Out Parameters When invoking a method that requires an out parameter, a discard can be passed inline to satisfy the method signature without declaring a local variable to hold the output.
// The boolean return value is evaluated, but the parsed integer is discarded
if (int.TryParse(inputString, out _))
{
    // Execution logic
}
Pattern Matching In switch statements and switch expressions, the discard acts as a wildcard or default catch-all pattern. It matches any value, including null, and is evaluated only if no preceding patterns match.
string typeDescription = entity switch
{
    Customer c => "External",
    Employee e => "Internal",
    _ => "Unclassified" // Discard pattern matching any other value, including null
};
Standalone Assignment A discard can be used as the target of an assignment operation. This explicitly signals to the compiler that the result of an expression returning a value is intentionally ignored.
// Discarding the Task returned by an asynchronous operation
_ = Task.Run(() => ProcessQueueAsync());
Lambda Parameters Introduced in C# 9.0, discards can be used as input parameters in lambda expressions or anonymous methods. This allows multiple unused parameters to be discarded using the same _ token without causing identifier collision errors.
// Discarding both the sender and event arguments
button.Click += (_, _) => Console.WriteLine("Button clicked");

Identifier Resolution Rules

The C# compiler applies specific binding rules to the _ token. If a standard variable named _ is explicitly declared and is currently in scope, standalone assignments (_ = ...) and implicitly typed out parameters (out _) will resolve to that local variable rather than acting as a discard. However, the _ token is always treated as a discard in specific syntactic contexts, regardless of whether a local variable named _ is currently in scope. These contexts include:
  • Deconstruction declarations (var (_, _) = ...)
  • Pattern matching (_ =>)
  • Explicitly typed out parameters (out var _ or out int _)
  • Lambda parameters (when multiple _ tokens are used)
int _ = 10; // Explicit variable declaration
_ = 20;     // Reassignment of the variable '_', NOT a discard

// To force a discard for an out parameter when '_' is in scope, 
// type declaration syntax must be used:
int.TryParse("123", out var _); // Acts as a discard
int.TryParse("123", out int _); // Acts as a discard

// Omitting the type declaration binds to the existing local variable:
int.TryParse("123", out _); // Overwrites the local variable '_' with 123
To maintain the integrity and readability of the discard pattern, explicit declaration of variables named _ is strongly discouraged in C# codebases.
Master C# with Deep Grasping Methodology!Learn More