Is there an easier way to convert List<> to ValueTuple<> #125551
-
|
I have a helper function like this, and it's best type parameter is like public static T? TryParseTuple<T>(Dictionary<string, object?> dict, string key, params char[] separator)
where T : struct, ITuple {
if (!dict.TryGetValue(key, out object? value) || value is not string str)
return null;
var data = str.Split(separator, StringSplitOptions.RemoveEmptyEntries);
return data.Length switch {
1 => ValueTuple.Create(data[0]),
2 => (data[0], data[1]),
3 => (data[0], data[1], data[2]),
...
};
}I want to know if there's an easier way to convert an unknown length |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
No. There is no way to express an unknown variety of tuple because they are totally different types, and considered unrelated in CLR type system. You have to write your own helper to switch on length.
This will never be possible. Names of tuple elements don't really exist at all. |
Beta Was this translation helpful? Give feedback.
No. There is no way to express an unknown variety of tuple because they are totally different types, and considered unrelated in CLR type system. You have to write your own helper to switch on length.
This will never be possible. Names of tuple elements don't really exist at all.