C# Discard Lambda Parameter: When and How to Use It
Learn how C# 9 lambda discard parameters use `_` for unused inputs, when they improve clarity, and how they apply to delegates, event handlers, and LINQ.

Using _ as a lambda parameter is a common C# idiom for an intentionally unused parameter. Lambda discard parameters, available in C# 9.0 and later, reduce noise in code where a delegate signature requires a parameter you do not need. They also make the intent explicit: the parameter exists only to satisfy the signature.
The Syntax of a Discard Lambda Parameter
A discard parameter is written as _ in the parameter list. When the lambda body does not use that parameter, the compiler treats _ as a placeholder that cannot be read or used inside the lambda body.
Func<int, int, int> add = (a, _) => a + 10; // second parameter is discardedIn this example, the second parameter is ignored. The lambda still accepts two arguments, but only the first is used. You can also discard all parameters:
Action<int, string> log = (_, _) => Console.WriteLine("Log entry");Each _ is an independent discard. Multiple discards in the same lambda are allowed, and each one represents a separate unused parameter.
Why Use a Discard Instead of a Named Parameter
Before lambda discard parameters, developers often named unused parameters with a leading underscore or a placeholder such as unused. That approach left a named parameter that could be referenced accidentally, and it did not tell the compiler that the parameter was intentionally unused.
A discard removes the parameter from usable scope: because the parameter cannot be read when it is treated as a discard, there is no risk of accidental use. It also makes the code's intent clear to other developers: the parameter is irrelevant to the operation.
Common Scenarios: Event Handlers and Delegates
Event handlers often have a signature with parameters that you do not need. For example, a button click handler receives sender and EventArgs, but you may only care that the click happened. Using discards keeps the handler concise.
button.Click += (_, _) => StartProcessing();Here, both parameters are discarded because the handler does not use them. This is clearer than writing (object sender, EventArgs e) and then not using them. The same applies to custom delegates where you must match a signature but only need a subset of the arguments.
public delegate void ProgressHandler(int percent, string status);
ProgressHandler onProgress = (_, status) => Console.WriteLine(status);Discards in LINQ and Collection Operations
LINQ methods often provide an index parameter that you do not need. For instance, Select has an overload that supplies the element index. If you only need the element itself, you can discard the index.
var doubled = numbers.Select((n, _) => n * 2);Similarly, Where with an index can discard it:
var even = numbers.Where((n, _) => n % 2 == 0);Discards also work in List<T>.ForEach when you need to iterate but ignore the element. List<T>.ForEach expects an Action<T>, so you can write list.ForEach(_ => Console.WriteLine("Item")) to ignore the element entirely. This is useful when the iteration itself has side effects that do not depend on the current item.
What Discards Are Not: Limitations and Pitfalls
A discard is not a variable. When _ is treated as a discard, you cannot read its value, pass it as an argument, or use it in an expression. However, the compiler decides whether _ is a discard by looking at whether the lambda body uses it. If the body uses _, the parameter is an ordinary parameter name. For example:
Func<int, int> f = _ => _ + 1; // valid: _ is an ordinary parameter hereThe lambda above compiles because _ is used in the body. A lambda like (_, _) => Console.WriteLine("Log entry") compiles because neither _ parameter is used, so the compiler can treat both as discards. If you write a lambda with two _ parameters and then try to use _ in the body, the code will not compile.
Another limitation is that _ is not always a discard. Outside a lambda parameter list, _ can be a regular identifier, such as a local variable. Inside a lambda, a parameter named _ may also shadow an outer variable named _ if the body uses the parameter. For example:
int _ = 5;
Func<int, int> f = value => value + _; // uses the outer _The meaning of _ is contextual. In a lambda parameter list, a _ parameter is a discard only when the body does not reference it. This keeps existing code that uses _ as an ordinary parameter name working.
Maintainability and Code Clarity Considerations
Using discards improves readability when the parameter is genuinely unused, but overusing them can harm clarity. If a parameter is unused now but might be used later, a named parameter may be more appropriate. A discard signals that the parameter is permanently irrelevant, which can mislead future maintainers if the delegate signature changes.
Consider the tradeoff in each case. For a one-off event handler where you never expect to use the sender, a discard is fine. For a lambda that might need the parameter in the next iteration, a named parameter may be better. The choice is mainly about what the code communicates.
Using discards has no extra runtime cost. They are compile-time constructs; the generated delegate still has the same signature, but the ignored values are not read. You can use them without worrying about performance overhead.
Compatibility and Version Requirements
General discards were introduced in C# 7.0, but lambda discard parameters specifically require C# 9.0 or later. If you compile with an older language version, _ in a lambda parameter list is an ordinary parameter name, so multiple _ parameters are not allowed. Most modern .NET projects use C# 9.0 or later, so this is rarely a constraint. Legacy projects that must use an older compiler can use named parameters instead.
The _ character is also used for other purposes, such as a wildcard in pattern matching or a discard in out parameters. The same syntax applies, but the context determines the meaning. In a lambda parameter list, _ is a discard when the lambda does not use it in its body.
Choosing Between Discard and Named Parameter
Use a discard when the parameter is not used and will not be used in the foreseeable future. Use a named parameter when the parameter might be used later, or when the parameter name carries information that helps explain the lambda's behavior. For example, in a LINQ Select that ignores the index, a discard is appropriate. If you plan to use the index in a future change, naming it index makes the code more self-documenting.
A practical rule is to prefer discards for parameters that are part of a delegate signature but irrelevant to the operation. For parameters that are relevant to the operation's logic, even if currently unused, a named parameter with a comment is safer. This keeps the code honest about its future intentions and avoids forcing a rename when the parameter becomes necessary.
Try matching browser tools
All toolsSmall utilities that fit C# work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related C# guides
All C# guidesC# Dispose vs Destructor: Key Differences Explained
c# dispose vs destructor: Understand the difference between Dispose and destructors in C#, when each runs, and how to manage unmanaged resources correctly.
C# Variable Declaration: Syntax and Practical Usage
Learn how to declare variables in C# with explicit types, var, and nullable annotations, and understand how defaults, scope, and readability affect your choices.
C# Primary Constructors for Classes: Syntax and Usage
Learn how to use C# primary constructors for classes: syntax, initialization, dependency injection, and limitations with practical examples.
C# IComparer: Custom Sorting with IComparer<T>
Learn how to implement IComparer<T> in C# to define custom sort order for arrays and lists, handle nulls, and avoid common performance pitfalls.
More in C#
All C# guidesThe guides filed next to this one in C#, in library order.