How to create a List of ValueTuple?
Asked Answered
K

4

43

Is it possible to create a list of ValueTuple in C# 7?

like this:

List<(int example, string descrpt)> Method()
{
    return Something;
}
Kamat answered 29/5, 2017 at 21:25 Comment(4)
Why don't you try?Sontag
I'm almost tempted to downvote since this is the syntax required to return a list of named tuples. Why the question?Narah
I suppose the question was about not the return-type of the Method, but rather, about the missing Something.Envisage
Related post - How to easily initialize a list of Tuples?Curitiba
F
110

You are looking for a syntax like this:

List<(int, string)> list = new List<(int, string)>();
list.Add((3, "first"));
list.Add((6, "second"));

You can use like that in your case:

List<(int, string)> Method() => 
    new List<(int, string)>
    {
        (3, "first"),
        (6, "second")
    };

You can also name the values before returning:

List<(int Foo, string Bar)> Method() =>
    ...

And you can receive the values while (re)naming them:

List<(int MyInteger, string MyString)> result = Method();
var firstTuple = result.First();
int i = firstTuple.MyInteger;
string s = firstTuple.MyString;
Fructify answered 29/5, 2017 at 21:36 Comment(1)
It's better to name the fields in the method definition.Narah
S
13

Sure, you can do this:

List<(int example, string descrpt)> Method() => new List<(int, string)> { (2, "x") };

var data = Method();
Console.WriteLine(data.First().example);
Console.WriteLine(data.First().descrpt);
Sontag answered 29/5, 2017 at 21:32 Comment(0)
C
5

Just to add to the existing answers, with regards to projecting ValueTuples from existing enumerables and with regards to property naming:

You can still name the tuple properties AND still use var type inferencing (i.e. without repeating the property names) by supplying the names for the properties in the tuple creation, i.e.

var list = Enumerable.Range(0, 10)
    .Select(i => (example: i, descrpt: $"{i}"))
    .ToList();

// Access each item.example and item.descrpt

Similarly, when returning enumerables of tuples from a method, you can name the properties in the method signature, and then you do NOT need to name them again inside the method:

public IList<(int example, string descrpt)> ReturnTuples()
{
   return Enumerable.Range(0, 10)
        .Select(i => (i, $"{i}"))
        .ToList();
}

var list = ReturnTuples();
// Again, access each item.example and item.descrpt
Catinacation answered 18/6, 2019 at 19:7 Comment(0)
O
0

This syntax is best applied to c# 6 but can be used in c# 7 as well. Other answers are much more correct because the are tending to use ValueTuple instead of Tuple used here. You can see the differences here for ValueTuple

List<Tuple<int, string>> Method()
{
   return new List<Tuple<int, string>>
   {
       new Tuple<int, string>(2, "abc"),
       new Tuple<int, string>(2, "abce"),
       new Tuple<int, string>(2, "abcd"),
   };
}

List<(int, string)> Method()
{
   return new List<(int, string)>
   {
       (2, "abc"),
       (2, "abce"),
       (2, "abcd"),
   };
}
Ocrea answered 29/5, 2017 at 21:29 Comment(12)
No need to use "Tuple" at allSontag
I surely prefer this sintax, is more readable and clear than the others.Auric
Upvoted. This is the correct syntax for C# 6 and below.Fructify
@Fructify what? The OP asked specifically about C# 7Sontag
@CamiloTerevinto Yes, but this also compiles in C#7, with a older syntax. So, it is one possible answer.Fructify
@Fructify this completely breaks the naming of "example" and "descrpt" which are the new features of c# 7, so no, it is not an answerSontag
@CamiloTerevinto the answer is correct so I am not sure if he may know or may not know the syntax of creating tuples, that`s why I answered in that way, if you can provide a better answer go ahead and I will vote it up, but voting an answer as wrong just because you can omit a Tuple word does not make sense to me!Ocrea
@RajmondBurgaj as I explained above, your version breaks the C# 7 feature of naming the parts of the Tuple. If you don't know C# 7 I'd suggest you not to answerSontag
Downvoted as this answer is using Tuple, not ValueTuple.Guadalupe
There are significant difference between Tuple and ValueTuple in terms of memory usage and performance. When people ask about C# 7 and tuples, they don't mean the old Tuples. A Tuple is created on the heap and requires garbage collection. A ValueTuple is created on the stack.Narah
Thanks for noting that @PanagiotisKanavos, you are right, I had to see the differences about ValueTuple and Tuple, I will add to the answer the difference as wellOcrea
The question is about c# 7Goldfish

© 2022 - 2024 — McMap. All rights reserved.