How can I add items to named Tuple
Asked Answered
A

2

5

I have the following named list of Tuple

var tupleList = new List<(string SureName, string LastName, int Age)>();

How can I add items to that list using tupleList.Add ?

Accumulate answered 8/10, 2021 at 7:12 Comment(0)
C
13
var tupleList = new List<(string SureName, string LastName, int Age)>();
tupleList.Add(("a", "b", 3));

enter image description here

As you can see it demanded an item at the end of add, which seems to be optional and the reason for the double brackets.

Casady answered 8/10, 2021 at 7:21 Comment(1)
I have no idea what the sentence "As you can see it demanded an item at the end of add, which seems to be optional and the reason for the double brackets." means. A better explanation is that ("a", "b", 3) is the syntax for a 3-item tuble. By adding whitespace, we can see the syntax more clearly: tupleList.Add( ("a", "b", 3) );. Or we could split into two lines: var tuple = ("a", "b", 3); tupleList.Add(tuple);Naevus
M
0
var tupleList = new List<Tuple<string, string, int>>();
tupleList.Add(new Tuple<string, string, int>("Nicolas", "Smith", 32));
Molly answered 8/10, 2021 at 7:20 Comment(2)
That's using Tuple - the OP is asking about ValueTuple, despite the title. (A "named tuple" always ends up meaning ValueTuple.)Zinc
Additionally, providing documentation and an synopsis of said documentation can be very helpful to future readers in understanding how this solution works.Elseelset

© 2022 - 2024 — McMap. All rights reserved.