C# tutorials > Modern C# Features > C# 6.0 and Later > What are discards (`_`) and how are they used?
What are discards (`_`) and how are they used?
_
). Discards are particularly useful when deconstructing tuples or objects, ignoring out parameters from methods, and when handling events. This tutorial explores the concept of discards, their use cases, and provides practical examples.
Basic Discard Usage
_
) acts as a discard. In the first example, when deconstructing a tuple, the second element (person's name) is assigned to the discard, effectively ignoring it. In the second example, the TryParse
method requires an out
parameter, but the parsed integer value isn't needed; therefore, it's assigned to a discard. The TryParse
still executes, verifying if the conversion is successful, but the parsed value is ignored.
// Example of using discards with tuples
(int age, string _, string city) = GetPersonInfo(); // Ignoring the person's name
Console.WriteLine($"Age: {age}, City: {city}");
// Example of using discards with out parameters
if (int.TryParse("123", out _))
{
Console.WriteLine("Successfully parsed an integer, but its value is discarded.");
}
Concepts Behind the Snippet
Real-Life Use Case Section
DataReceived
event. The event handler OnDataReceived
ignores the sender (object sender
) using a discard because it only cares that the event occurred, not about the object that raised it.
//Ignoring event sender.
public class MyClass
{
public event EventHandler DataReceived;
public void SimulateDataReception()
{
DataReceived?.Invoke(this, EventArgs.Empty);
}
}
public class ConsumerClass
{
public ConsumerClass(MyClass myObject)
{
myObject.DataReceived += OnDataReceived;
}
private void OnDataReceived(object _ , EventArgs e)
{
Console.WriteLine("Data received!");
}
}
Best Practices
Interview Tip
out
parameters, and event handling.
When to use them
out
parameter, but you don't need the returned value.
Memory Footprint
Alternatives
unused
, dummy
, or something similar. While these worked, they didn't explicitly convey the intent of *discarding* the value. They also could trigger compiler warnings if the variable wasn't used. Another alternative, especially for out
parameters, is to simply not call the method at all if you don't need the output. However, this is only possible if the method's side effects are not required.
Pros
out
parameters.
Cons
FAQ
-
Can I use multiple discards in the same scope?
Yes, you can use multiple discards in the same scope. The compiler treats all discards as the same storage location. -
Are discards variables?
No, discards are not variables in the traditional sense. They are write-only locations that explicitly indicate you're discarding a value. You cannot read from a discard. -
Do discards allocate memory?
Discards are designed to be memory-efficient. The compiler typically doesn't allocate separate memory locations for each discard. All discards in a scope often share the same memory location. -
What C# version introduced Discards?
Discards were introduced in C# 7.0.