Tuples

Tuples (System.Tuple) 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.

Example:1 
var tuple = new Tuple<string, string[], int>("perl",new string[] { "java", "c#" },1);
 
// Tuple object Items can be accessed using "item" Property 
Console.WriteLine(tuple.item1);
 
Example:1 
public Tuple<int, int> GetDivAndRemainder(int i, int j) 
{
return Tuple.Create(i/j, i%j);
}
 


Leave a Reply