Thursday 13 June 2013

C# Tuple



A tuple is a data structure that has a specific number and sequence of elements. An example of a tuple is a data structure with three elements (known as a 3-tuple or triple) that is used to store an identifier such as a person's name in the first element, a year in the second element, and the person's income for that year in the third element. The .NET Framework directly supports tuples with one to seven elements. In addition, you can create tuples of eight or more elements by nesting tuple objects in the Rest property of a Tuple<T1, T2, T3, T4, T5, T6, T7, TRest> object.
Tuple is a typed, immutable and generic construct. It is a useful container for storing conceptually related data. A simple class with commented members and helper methods is more useful for important things.
Example:
var population = new Tuple<string, int, int, int, int, int, int>(
                           "New York", 7891957, 7781984,
                           7894862, 7071639, 7322564, 8008278);
Creating the same tuple object by using a helper method is more straightforward, as the following example shows.

var population = Tuple.Create( 
"New York", 7891957, 7781984, 7894862, 7071639, 7322564, 8008278);

No comments:

Post a Comment