Discards are placeholders that can be used to ignore the result of an expression or a method call. They are represented by an underscore character _ and can be used in place of a variable name when you don't need the value returned by an expression or a method call.
They are useful in situations where you need to call a method that returns a value, but you don't actually care about the value.
string[] dateStrings = {"05/01/2018 14:57:32.8", "2018-05-01 14:57:32.8", "2018-05-01T14:57:32.8375298-04:00", "5/01/2018",
"5/01/2018 14:57:32.80 -07:00", "1 May 2018 2:57:32.8 PM", "16-05-2018 1:00:32 PM", "Fri, 15 May 2018 20:10:57 GMT" };
foreach (string dateString in dateStrings)
{
if (DateTime.TryParse(dateString, out _))
Console.WriteLine($"'{dateString}': valid");
else
Console.WriteLine($"'{dateString}': invalid");
}
// The example displays output like the following:
// '05/01/2018 14:57:32.8': valid
// '2018-05-01 14:57:32.8': valid
// '2018-05-01T14:57:32.8375298-04:00': valid
// '5/01/2018': valid
// '5/01/2018 14:57:32.80 -07:00': valid
// '1 May 2018 2:57:32.8 PM': valid
// '16-05-2018 1:00:32 PM': invalid
// 'Fri, 15 May 2018 20:10:57 GMT': invalid
A tuple is an object that contains a collection of related values, which can be of different data types. A tuple is similar to an array or a list, but it's designed to represent a fixed number of elements with specific types, rather than a variable number of elements.
var (_, _, _, pop1, _, pop2) = QueryCityDataForYears("New York City", 1960, 2010);
Console.WriteLine($"Population change, 1960 to 2010: {pop2 - pop1:N0}");
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 (year2 == 2010) population2 = 8175133;
return (name, area, year1, population1, year2, population2);
}
return ("", 0, 0, 0, 0, 0);
}
// The example displays the following output:
// Population change, 1960 to 2010: 393,149