use of Tuple concepts in c#
Why we are using Tuple concepts instead of Dictionary or list?
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
VulpesPosted Jan 7, 2014, 9:10 AM
Dhirendra MisraPosted Jan 7, 2014, 3:34 AM
A Tuple has many items. Each item can have any type. The Tuple class provides a unified syntax for creating objects with typed fields. Once created, the fields in the Tuple cannot be mutated. This makes the Tuple similar to a value type.
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
Tuples are commonly used in four ways:
Examples to create tuple:
// Create a 7-tuple.
var population = new Tuple<string, int, int, int, int, int, int>( "New York", 7891957, 7781984, 7894862, 7071639, 7322564, 8008278); // Display the first and last elements.
Console.WriteLine("Population of {0} in 2000: {1:N0}", population.Item1, population.Item7);
Using Create Method:
// Create a 7-tuple.
var population = Tuple.Create("New York", 7891957, 7781984, 7894862, 7071639, 7322564, 8008278); // Display the first and last elements.
Console.WriteLine("Population of {0} in 2000: {1:N0}", population.Item1, population.Item7);
Performance compared with keyValuePair:http://www.dotnetperls.com/tuple-keyvaluepair