A tuple is a data structure that has a specific number and
sequence of elements introduced in .net Framework 4. That
is, a Tuple<int, string, int> is a tuple that contains
exactly three items: an int, followed by a string,
followed by an int. The sequence is important not only to
distinguish between two members of the tuple with the same type, but also for
comparisons between tuples.
Some people tend to love
tuples because they give you a quick way to combine multiple values into one
result.
Tuples are commonly
used in four ways:
- To represent a single set of data. For example, a tuple
can represent a database record, and its components can represent
individual fields of the record.
- To provide easy access to, and manipulation of, a data
set.
- To return multiple values from a method without using out parameters
(in C#) or ByRef parameters (in Visual Basic).
- To pass multiple values to a method through a single parameter. For example, the Thread.Start(Object) method has a single parameter that lets you supply one value to the method that the thread executes at startup time. If you supply a Tuple<T1, T2, T3> object as the method argument, you can supply the thread’s startup routine with three items of data.
Examples
The following example
creates an 8-tuple (octuple) that contains prime numbers that are less than 20.
var population = new Tuple<string, int, int, int, int, int, int, long>(
"New York",
7891957, 7781984,
7894862, 7071639, 7322564, 8008278, 80);
No comments:
Post a Comment