Why use a 1-tuple, Tuple<T1> in C#? [duplicate]

烈酒焚心 提交于 2019-12-10 14:07:33

问题


I don't see the use of 1-tuples in C#. Why do programmers use them?

Tuple Class

I have seen a declaration like the following:

Tuple<string> state;

But I wonder if there are further uses.


回答1:


The Tuple classes only go up to 7. When you need an 8-tuple, you have to use

Tuple<T1, T2, T3, T4, T5, T6, T7, Tuple<T8>>

But if you need 8 items, you probably want to redesign your application.

You can go as far as you want, as long as you chain tuples together. 15 items:

Tuple<T1, T2, T3, T4, T5, T6, T7,
    Tuple<T8, T9, T10, T11, T12, T13, T14, Tuple<T15>>>

Note that C# will throw a runtime exception if the last item (TRest) is not a tuple (since it cannot be enforced at compile-time because the ITuple interface is internal)



来源:https://stackoverflow.com/questions/31134139/why-use-a-1-tuple-tuplet1-in-c

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