I\'ve just come across discards in c# and am wondering a couple things that I feel Microsoft Docs on Discards didn\'t explain very well.
Discards are equivalent to unassigned variables; they do not have a value. Because there is only a single discard variable, and that variable may not even be allocated storage, discards can reduce memory allocations. Because they make the intent of your code clear, they enhance its readability and maintainability. They are basically a way to ignore local variables those are irrelevant to perform the action, it is like when you call a method that returns a value, however, you are interested only in the underlying operations it performs, you don't use the return of that method.
As we can see on the example below, discards are particularly useful in working with tuples when your application code uses some tuple elements but ignores others:
Note: I have got the example from Microsoft docs and I have included a second output just to show the difference
public class Example
{
public static void Main()
{
var (_, _, _, pop1, _, pop2) = QueryCityDataForYears("New York City", 1960, 2010);
var (cityName, _, year1, pop3, _, pop4) = QueryCityDataForYears("New York City", 1980, 2010);
Console.WriteLine($"Population change, in 1960 to 2010: {pop2 - pop1:N0}");
Console.WriteLine($"Population change, in {cityName} from {year1} to 2010: {pop4 - pop3:N0}");
}
private static (string, double, int, int, int, int) QueryCityDataForYears(string name, int year1, int year2)
{
int population1 = 0, population2 = 0;
double area = 0;
if (name == "New York City")
{
area = 468.48;
if (year1 == 1960)
{
population1 = 7781984;
}
if (year1 == 1980)
{
population1 = 7981984;
}
if (year2 == 2010)
{
population2 = 8175133;
}
return (name, area, year1, population1, year2, population2);
}
return ("", 0, 0, 0, 0, 0);
}
}
//Output
//Population change, in 1960 to 2010: 393,149
//Population change, in New York City from 1980 to 2010: 193,149