Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# 7.0 discard out parameter ambiguity [duplicate]

Tags:

c#

out

c#-7.0

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
like image 650
budi Avatar asked Jun 28 '17 16:06

budi


1 Answers

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

  • as an identifier in a "designator" (declaration expression), which includes out parameters and deconstruction of tuples and similar
  • as an indentifier in pattern matching (switch ... case int _)
  • apparently also for declaring anonymous delegates that has to take parameters in order to fit a delegate type but where you don't require the actual parameter.

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.

like image 110
Lasse V. Karlsen Avatar answered Sep 22 '22 15:09

Lasse V. Karlsen