From https://blogs.msdn.microsoft.com/dotnet/2017/03/09/new-features-in-c-7-0/:
We allow "discards" as out parameters as well, in the form of a _, to let you ignore out parameters you don’t care about:
p.GetCoordinates(out var x, out _); // I only care about x
Consider the following code:
void Foo(out int i1, out int i2)
{
i1 = 1;
i2 = 2;
}
int _;
Foo(out var _, out _);
Console.WriteLine(_); // outputs 2
Questions:
Why is the "discard" out parameter being outputted in this context?
Also, shouldn't there be an "already defined in scope" error for out var _
?
int i;
Foo(out var i, out i);
Console.WriteLine(i); // Error: A local variable or function named 'i'
// is already defined in this scope
Here's the GitHub issue that details the behavior of discards, the relevant parts are:
However, semantically we want this to create an anonymous variable, and shadow any true variable (e.g. parameter or field) from an enclosing scope named _.
...
We have to be careful with these changes so that any program that uses _ as an identifier and is legal today continues to compile with the same meaning under these revised rules.
_
only means "discard this parameter" if you declare it and use it
out
parameters and deconstruction of tuples and similarswitch
... case int _
)The out var _
is one of these into this category.
However, int _;
is a separate statement, and thus this signals that you "care" about this variable and thus it isn't a discard.
Thus you get a normal variable with the name _
, which is still legal. This gets the value 2 from the method call, as expected.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With