A Tuple is the counterpart of the list.
While List stores 0-N of the same type, Tuple stores 1-M (possibly) different types of elements, where N is unbounded and M is statically fixed / defined.
Each of these elements can be accessed in a strongly typed manner by name (or “index”, as happens for alignment).
Thus, they look like an anonymous type (in fact, it is more likely a “record” rather than a “tuple”, because names can be arbitrarily selected):
new { _0 = value0, _1 = value1, }
But Tuple types are nominatively typed (a bunch of different classes support them, like t21 or Func
), and therefore a specific Tuple type can be explicitly specified (for example, in method signatures), which cannot be used for an anonymous type.
Although I would say that the practical use of Tuples in C # is hampered by the lack of support (for example, without decomposition, without an application, etc.), they are always used in languages like Scala. C #'s approach usually is to "create a new named type", but introduce Tuple
types as another available tool.
(A great place where Tuples are very useful is intermediate calculations, but C # has anonymous types, which, as seen from LINQ, perform this role pretty well in most cases when the calculations are performed by the same method.)
user166390
source share