C# and underscores
TL;DR
- an underscore in c# is a variable that you do not care about; they are called discards
- an underscore is a digit separator
Underscores as discards
Shane Rounce
So if you see code like this:
var (_, _, _, pop1, _, pop2) = QueryCityDataForYears("New York City", 1960, 2010);
The 1st, 2nd, 3rd & 5th values of the returned tuple; we don't care about. Discards were introduced in c# 7.0
This example is from: https://docs.microsoft.com/en-gb/dotnet/csharp/whats-new/csharp-7?WT.mc_id=ondotnet-c9-cxa#discards
Discard refs:
- https://channel9.msdn.com/Shows/On-NET/CSharp-Language-Highlights-Discards
- https://docs.microsoft.com/en-gb/dotnet/csharp/discards
- https://docs.microsoft.com/en-gb/dotnet/csharp/whats-new/csharp-7?WT.mc_id=ondotnet-c9-cxa#discards
Underscore as a number separator
Simon Rae
Or more formally ‘digit separator’… again as of c#7 an underscore can also be used when assigning a numeric value. It is syntactic sugar
int distanceToMoon = 384_400; // same as int distanceToMoon = 384400
Just makes it easier to read
NB. this represents UK number styling.
Read other posts