Tuples are intended to be just a bag of variables. As variables, you can assign any value assignable to the variable type regardless of the variable name.
The names are only an indication as variable names are. The only difference in return values is that the compiler persists the name of the tuple elements using TupleElementNames attribute.
In fact, even in the presence of the names, the compiler warns you if you don't use the same names as, usually, it's a mistake and still valid syntax:
(string firstname, string lastname, int id) NextEmployee()
=> (apples: "jamie", potatos: "hince", oranges: 102348);
/*
Warning CS8123 The tuple element name 'apples' is ignored because a different name is specified by the target type '(string firstname, string lastname, int id)'.
Warning CS8123 The tuple element name 'potatos' is ignored because a different name is specified by the target type '(string firstname, string lastname, int id)'.
Warning CS8123 The tuple element name 'oranges' is ignored because a different name is specified by the target type '(string firstname, string lastname, int id)'.
*/
The syntax your using here:
(string lastname, var _, int __) = NextEmployee();
is not tuple declaration syntax, but tuple deconstruction syntax that creates a LastName
variable, a _
variable and a __
variable.
These are all equivalents that produce the same result:
(var lastname, var _, var __) = NextEmployee(); // or any combination of
varand type names
var (lastname, _, __) = NextEmployee();
To declare a tuple to receive the return of the method, you would need to declare a tuple variable:
(string firstname, string lastname, int id) t = NextEmployee();
var t = NextEmployee();
But it seems your intent is to ignore the LastName
and id
values:
(_, string lastname, _) = NextEmployee(); // declares a lastname variable and ignores firstname and id
But if you really write (string lastname, _, _) = NextEmployee();
, then you are assigning a local string variable named lastname
with the value of the returned string "variable" firstname
.
Just remember, tuples are not entities. They are sets of values. If the library you are using uses tuples as entities, be aware that chances are something else might be wrong with that library.