Search code examples
.nettuplesc#-7.0valuetuple

Is there a shorthand way to convert a Tuple to a ValueTuple?


I have just included a DLL file into my project and am using a method which returns a List<Tuple>. I have only recently became aware of the ValueTuple datatype in C# 7 and want to use it instead of the normal Tuples which are returned. I am aware that I could loop through and do the conversion manually however I would like to avoid doing this if possible and was wondering if anyone was aware of a shorthand way to casting a Tuple to a ValueTuple? If I am being ignorant and this isn't possible then I would be grateful if someone could make me aware of the most performant way I could achieve this conversion.

I have looked for a .ToValueTuple() method and using a basic cast on the function returning the List with no joy.

When attempting:

List<(string entityName, int min, int average, int max)> tupTest = trs.GetEntityStats();

I receive the error:

Cannot convert source type 'System.Collections.Generic.List>' to target type 'System.Collections.Generic.List<(string entityName, int min, int average, int max)>'


Solution

  • The TupleExtensions Class has ToTuple and ToValueTuple methods.