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
Foo()
, it should most likely beFoo(out _);
, though I may be completely misinterpreting your problem statement. – Subsellium_
is a valid name for a variable. – Uptown_
is treated as a discard and when it isn't. TL&DR: if you change the line toFoo(out var _, out var _);
then both parameters are treated as discards even when a_
variable is in scope.out _
is only treated as a discard when no_
is in scope, otherwise it refers to the variable. – Dithyramb