43

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

like this:

List<(int example, string descrpt)> Method()
{
    return Something;
}
Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
ArthNRick
  • 925
  • 1
  • 7
  • 22

4 Answers4

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;
Guilherme
  • 5,143
  • 5
  • 39
  • 60
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);
Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
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
StuartLC
  • 104,537
  • 17
  • 209
  • 285
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"),
   };
}
Rey
  • 3,663
  • 3
  • 32
  • 55
  • 2
    No need to use "Tuple" at all – Camilo Terevinto May 29 '17 at 21:32
  • 1
    I surely prefer this sintax, is more readable and clear than the others. – Gusman May 29 '17 at 21:39
  • 2
    Upvoted. This is the correct syntax for C# 6 and below. – Guilherme May 29 '17 at 21:42
  • @Guilherme what? The OP asked specifically about C# 7 – Camilo Terevinto May 29 '17 at 21:45
  • @CamiloTerevinto Yes, but this also compiles in C#7, with a older syntax. So, it is one possible answer. – Guilherme May 29 '17 at 21:49
  • 1
    @Guilherme this completely breaks the naming of "example" and "descrpt" which are the new features of c# 7, so no, it is not an answer – Camilo Terevinto May 29 '17 at 21:50
  • @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! – Rey May 29 '17 at 21:50
  • @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 answer – Camilo Terevinto May 29 '17 at 21:51
  • 7
    Downvoted as this answer is using `Tuple`, not `ValueTuple`. – David Arno May 30 '17 at 07:19
  • 7
    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. – Panagiotis Kanavos May 30 '17 at 13:32
  • 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 well – Rey May 30 '17 at 13:52
  • 1
    The question is about c# 7 – Osama AbuSitta Jun 01 '17 at 10:04