Search code examples
c#tuplesnamedreference-typec#-7.0

How to create named reference-type tuples?


The following line creates a named ValueTuple:

var tuple = (a:1, b:2, c:3, d:4, e:5, f:6);  

Value types can not be passed around efficiently. Does C# 7 offer a way to create named tuples of the Tuple type?


Solution

  • If you mean if there's a way to attach other names to the properties of System.Tuple<...> instances, no there isn't.

    Depending on why you want it, you might get around it by converting System.Tuple<...> instances to System.ValueTuple<...> instances using the ToValueTuple overloads in TupleExtensions and back using the ToTuple overloads.

    If you don't really need the tuples, you can deconstruct them into discrete variables using the Deconstruct overloads or the var (v1, .., vn) = tuple deconstruction syntax.