Tuesday 28 January 2014

Tuple in c#

In this article we are going to see a concept called Tuple, which is present in the dotnet framework 4.0, why Tuple is there in framework, what is the usage of the That ? Tuple is a data structure to store the identifiers, The Tuple is stored in a separated location of a memory.Once we create the Tuple, it can be be changed.


Tuple:


Create<T1>(T1)
1Create<T1, T2>(T1, T2)
2Create<T1, T2, T3>(T1, T2, T3)
3Create<T1, T2, T3, T4>(T1, T2, T3, T4)
4Create<T1, T2, T3, T4, T5>(T1, T2, T3, T4, T5)
5Create<T1, T2, T3, T4, T5, T6>(T1, T2, T3, T4, T5, T6)
6Create<T1, T2, T3, T4, T5, T6, T7>(T1, T2, T3, T4, T5, T6, T7)
7Create<T1, T2, T3, T4, T5, T6, T7, T8>(T1, T2, T3, T4, T5, T6, T7, T8)

Example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{

    class Employee
    {
        public string EmpId { get; set; }

        public string Address { get; set; }

        public int PhoneNumber { get; set; }

        public string Department { get; set; }

        public string EmpName { get; set; }
    }


    class Program
    {
        static void Main(string[] args)
        {
            var Company = Tuple.Create<int, string, string, Employee>(1, "Rajesh", "Chennai", new Employee() { EmpId = "E1", EmpName = "Rajesh . G", Department = "Technical", PhoneNumber = 1233444, Address = "chennai" });    
       
            Console.WriteLine("Number:- {0}, Name:- {1}, Employee Department:- {2}", Company.Item1.ToString(), Company.Item2, Company.Item4.Department);           
            Console.Read();
        }
    }
}




Output:
Number:- 1, Name:- Rajesh, Employee Department:- Technical


From this article you can learn how to use the Tuple.


No comments:

Post a Comment