ValueTuple as a new feature in C# 7.0 is having public method Create
which helps to create ValueTuples (from singleton to octuple
or more) on the other hand we can also use new
to achieve the same results. I noticed these behave differently. I am trying to research is below implementation wrong or this is something as per design:
Method CreateOctuple()
is working as expected:
private static ValueTuple<int, int, int, int, int, int, int, ValueTuple<int>> CreateOctuple()
{
return new ValueTuple<int, int, int, int, int, int, int, ValueTuple<int>>(1, 2, 3, 4, 5, 6, 7, new ValueTuple<int>(8)); ;
}
Now, I tried to achieve same output using Create()
method, unfortunately, it is complaining about the return type:
private static ValueTuple<int, int, int, int, int, int, int, ValueTuple<int>> OctupleUsingCreate()
{
return ValueTuple.Create(1, 2, 3, 4, 5, 6, 7, ValueTuple.Create(8));
}
P.S. All packages are up-to-date and I am using Visual Studio 2017 -latest release.
As suggested by Svick
static ValueTuple<int, int, int, int, int, int, int, ValueTuple<int>> OctupleUsingCreate()
{
return ValueTuple.Create(1, 2, 3, 4, 5, 6, 7, 8);
}
The issue is that ValueTuple.Create
already takes care of calling ValueTuple.Create
on the 8th element. So while the correct type for 8-tuple is ValueTuple<int, int, int, int, int, int, int, ValueTuple<int>>
, you're creating ValueTuple<int, int, int, int, int, int, int, ValueTuple<ValueTuple<int>>>
. The fix is just to remove the second call to ValueTuple.Create
:
static ValueTuple<int, int, int, int, int, int, int, ValueTuple<int>> OctupleUsingCreate()
{
return ValueTuple.Create(1, 2, 3, 4, 5, 6, 7, 8);
}
Or you could just use the tuple syntax, but I'm assuming you have a reason to avoid it:
static (int, int, int, int, int, int, int, int) OctupleUsingCreate()
{
return (1, 2, 3, 4, 5, 6, 7, 8);
}