Tuesday, December 6, 2011

Tuple Class (.NET Framework 4)

Tuple Class (System.Tuple)

Web definitions
"A tuple (or n-tuple) is a fixed size collection of elements. Pairs, triples, quadruples etc. are tuples. In a programming language, a tuple is a data object containing other objects as elements. These element objects may be of different types."
"In mathematics, a tuple is a finite sequence (also known as an ordered list) of objects, each of a specified type. A tuple containing n objects is known as an n-tuple. For example the 4-tuple (or quadruple), with components of respective types PERSON, DAY, MONTH and YEAR, could be used to record that a certain person was born on a certain day of a certain month of a certain year."
"In set theory, an (ordered) n-tuple is a sequence (or ordered list) of n elements, where n is a positive integer. Tuples are usually written by listing the elements within parentheses "( )" and separated by commas; for example, (2,7,4,1,7) denotes a 5-tuple."
Introduction

A tuple is a data structure that has a specific number and sequence of elements of variety of types. 

The Tuple class does not itself represent a tuple. Instead, it is a factory class that provides static methods for creating instances of the tuple types that are supported by the .NET Framework. It provides helper methods that you can call to instantiate tuple objects without having to explicitly specify the type of each tuple component. You can also create an instance of a tuple class by calling its class constructor.

Tuple is a static class provides a Create method for creating the tuples of size 1-8. 
  • Tuple – Represents a tuple of size 1  
    • Create(T1)
  • Tuple – Represents a tuple of size 2 
    • Create(T1,T2)
  • Tuple – Represents a tuple of size 3 
    • Create(T1,T2,T3)
  • Tuple – Represents a tuple of size 4 
    • Create(T1,T2,T3,T4)
  • Tuple – Represents a tuple of size 5 
    • Create(T1,T2,T3,T4,T5)
  • Tuple – Represents a tuple of size 6 
    • Create(T1,T2,T3,T4,T5,T6)
  • Tuple – Represents a tuple of size 7 
    • Create(T1,T2,T3,T4,T5,T6,T7)
  • Tuple – Represents a tuple of size 8 or greater. 
    • Create(T1,T2,T3,T4,T5,T6,T7,T8)

Usage

Tuples are commonly used in four ways:
  • Represent a single set of data, a tuple can represent a database record.
  • Provide easy access to, and manipulation of, a data set. 
  • Using Tuple we can return multiple values from a method.
  • Similarly Tuple we can pass multiple values to a method through a single parameter. 
Example
 static void Main(string[] args) 
 { 
   // Create two-item tuple. Using constructors. 
   Tuple<int, string> tuple =
       new Tuple<int, string>(1, "Two Item Tuple"); 
  
   // Create two-item tuple. Using Create Method. 
   var tuple1 = Tuple.Create(1, "Two Item Tuple"); 
  
   // Access tuple properties. 
   Console.WriteLine("Tuple element 1: " + tuple.Item1); 
   Console.WriteLine("Tuple element 2: " + tuple.Item2); 
  
   //Try to set Item1 of tuple 
   //Error 1 Property or indexer 'System.Tuple<int,string>.Item1'
   //cannot be assigned to -- it is read only 
   //tuple.Item1 = 2; 
  
   Console.WriteLine("Tuple with two elements: "+tuple1); 
  
   //Create Tuples with element more than 8. Using constructors. 
   var tuple2 = new Tuple<string, int, int, int, int, int, int,
           Tuple<int, int, int, string>> 
         ("Number Items:", 1, 2, 3, 4, 5, 6, 
         new Tuple<int, int,int,string>(7, 8,9,"End of Items.")); 
  
  
   //Create Tuples with element more than 8. Using Create Method. 
   var tuple3 = Tuple.Create("Number Items:", 1, 2, 3, 4, 5, 6, 
   new Tuple<int, int, int, string>(7, 8, 9, "End of Items.")); 
  
  
  
   Console.WriteLine("Tuple with more than 8 elements: " + tuple3); 
        
   Console.ReadLine(); 
 }  
Output

Tuple element 1: 1
Tuple element 2: Two Item Tuple
Tuple with two elements: (1, Two Item Tuple)
Tuple with more than 8 elements: (Number Items:, 1, 2, 3, 4, 5, 6, (7, 8, 9, End
 of Items.))