Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

.net

tuples

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.

like image 608
Brk Avatar asked Jun 30 '15 09:06

Brk


1 Answers

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)

like image 99
Dennis_E Avatar answered Sep 29 '22 07:09

Dennis_E