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

对着背影说爱祢 提交于 2021-02-05 07:56:07

问题


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)>'


回答1:


The TupleExtensions Class has ToTuple and ToValueTuple methods.



来源:https://stackoverflow.com/questions/57149107/is-there-a-shorthand-way-to-convert-a-tuple-to-a-valuetuple

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!