Is it possible to create a list of ValueTuple in C# 7?
like this:
List<(int example, string descrpt)> Method()
{
return Something;
}
Is it possible to create a list of ValueTuple in C# 7?
like this:
List<(int example, string descrpt)> Method()
{
return Something;
}
Sure, you can do this:
You are looking for a syntax like this:
You can use like that in your case:
You can also name the values before returning:
And you can receive the values while (re)naming them:
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.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:
This syntax is best applied to
c# 6
but can be used inc# 7
as well. Other answers are much more correct because the are tending to useValueTuple
instead ofTuple
used here. You can see the differences here forValueTuple