0

I'm having a strange problem with Tuples and I'm not really sure what I'm overlooking. The attached screen shows my sample code and the output... note that the property names are ignored. This behavior is the same in Visual Studio.

My question is why, when defined with property names, does the Tuple end up at item1 and such?

enter image description here

The comments have made me realize I do not understand the situation with the identifiers... the debugger in VS 2019 shows itemx properties just as my original question but this code works:

var tupleList = new List<(int Index, string Name)>
{
    (1, "cow"),
    (5, "chickens"),
    (1, "airplane")
};

foreach (var tuple in tupleList)
    Console.WriteLine($"{tuple.Index} - {tuple.Name}");

The debugger shows Itemx properties but I'm able to access using the names selected. I need to look at the source and better understand how this is working... seeing itemx instead of my expected names drew me down a rabbit hole.

TY all for the discussion and apologies for breaking rules by posting a screen.

Chris

Chris Keller
  • 235
  • 3
  • 9
  • 2
    Please don't post images of code. Provide the code as plaintext instead. Also before you jump to the conclusion that the names are not preserved, have you actually tried _programmatically accessing_ the list of tuples first? – Patrick Roberts Nov 27 '19 at 23:24
  • Yes, I did. The behavior is the same in Visual Studio and LinqPad - pictured. – Chris Keller Nov 27 '19 at 23:25
  • 2
    That's just how tuples work, the names you give them in code are only syntactic sugar. If you want (in this case LinqPad) to output proper names, you will need to use a proper class. – DavidG Nov 27 '19 at 23:25
  • 3
    Apparently the debug tool you are using ignores the [`TupleElementNames` attribute](https://stackoverflow.com/a/43489034/11683). – GSerg Nov 27 '19 at 23:25
  • 1
    As a general rule, names do not *exist* at runtime*. Names are primarily there to allow compiler/human communication. Wich includes Debugger/Human and Error Messages/Human. Outside those roles, they serve no purpose. | *Caveat: Thanks to reflection, names do exist to some degree at runtime. But I consider reflection a too extreme case to worry about. – Christopher Nov 27 '19 at 23:26

1 Answers1

0

This is working as expected, as hinted in the comments from others.

I found this blog post helpful as well as the documentation and source code.

Chris Keller
  • 235
  • 3
  • 9