DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Data Engineering
  3. Data
  4. Tuple Type in C#4.0

Tuple Type in C#4.0

Pranay Rana user avatar by
Pranay Rana
·
Jan. 01, 13 · Interview
Like (0)
Save
Tweet
Share
4.26K Views

Join the DZone community and get the full member experience.

Join For Free

What is Tuple type ?
Tuple is new class type added in C#4.0. Tuple type allows to create data structure which is consist of specific type and specific number of elements. In the following post its about basic of Tuple type, how and where to use this new type in your code.

How to create Tuple type object
Following code show how you can create and use this class type.

Console.WriteLine("How to create and use");
Tuple<int, string> tuple = new Tuple<int, string>(1, "pranay");
Console.WriteLine(tuple.Item1 + "-" + tuple.Item2);

One way to create object is just make use of constructor and create the Tuple object.
Tuple<int, string> tuple1 = Tuple.Create(2, "ab1");
Console.WriteLine(tuple1.Item1 + "-" + tuple1.Item2);
Console.WriteLine();
Second way is make use of Create Static method supported by Tuple class and create object.

Refer Element of Tuple Object
As you see in both example element of Tuple get referred using name Item1 and Item2, so this is predefined name for the element by .net framework only. So to refer element in Tuple object you need to write "Item+number_of_the_element".

Constrain
You cannot change value of the the property of Tuple object.
// after creation of object this is not possible
// this gives compile time error
tuple1.Item1 = 100; 
C# allows to create Tuple object with the 7 different type element.
Create<T1>(T1) Creates a new 1-tuple, or singleton.
Create<T1, T2>(T1, T2) Creates a new 2-tuple, or pair.
Create<T1, T2, T3>(T1, T2, T3) Creates a new 3-tuple, or triple.
Create<T1, T2, T3, T4>(T1, T2, T3, T4) Creates a new 4-tuple, or quadruple.
Create<T1, T2, T3, T4, T5>(T1, T2, T3, T4, T5) Creates a new 5-tuple, or quintuple.
Create<T1, T2, T3, T4, T5, T6>(T1, T2, T3, T4, T5, T6) Creates a new 6-tuple, or sextuple.
Create<T1, T2, T3, T4, T5, T6, T7>(T1, T2, T3, T4, T5, T6, T7) Creates a new 7-tuple, or septuple.
What to do if you want to have more than 8 values ?
Last variation of Tuple creation allows to create Tuple object with more than 8 different element.
Create<T1, T2, T3, T4, T5, T6, T7, T8>(T1, T2, T3, T4, T5, T6, T7, T8) Creates a new 8-tuple, or octuple. 
in this T8 element is of type Tuple only that means if you want to create tuple more than 8 it will be like this
var num = new Tuple<int, int, int, int, int, int, int, 
                 Tuple<int>>(1, 2, 3, 4, 5, 6, 7, 
                 new Tuple<int>(8));
Use Of Tuple type
To Pass data To methods
Sometime we just create class or structure to just pass data to method, creation of this extra class or structure in code can be avoided by using Tuple object to pass data.
Following is example of this where object of Tuple used to pass int and string data. And its shows that we can also create list of Tuple object and pass to method for processing.
ProcessData(new Tuple<int, string>(1, "Pranay"));

List<Tuple<int, String>> lst = new List<Tuple<int, string>>();
lst.Add(new Tuple<int, string>(1, "Pranay"));
lst.Add(Tuple.Create(2, "ab1"));
lst.Add(Tuple.Create(1, "abcdef"));
lst.Add(Tuple.Create(3, "cd2"));
ProcessListOfData(lst);

//Process single Tuple object
public static void ProcessData(Tuple<int, string> tup)
{
    Console.WriteLine("ProcessData");
    Console.WriteLine(tup.Item1 + "-" + tup.Item2);
    Console.WriteLine();
}
//Process list of Tuple object
public static void ProcessListOfData(List<Tuple<int, String>> lst)
{
    Console.WriteLine("ProcessListOfData");
    var data = lst.Where(x => x.Item1 == 1).Select(x => x);
    foreach (var tup in data)
    {
       Console.WriteLine(tup.Item1 + "-" + tup.Item2);
    }
    Console.WriteLine();
}
To Return From the Method 
Tuple can be used to return data from method where you want to return more than one value or list of object without creating extra class or structure for carrying data.
Following is example of returning data type.
var data = ReturnTupleData();
Console.WriteLine(data.Item1 + "-" + data.Item2);

public static Tuple<int, string> ReturnTupleData()
{
    return new Tuple<int, string>(1, "pranay");
}
As you can see it use Tuple object to return int and string data which is not possible with the normal method because we only have one return type.
To Return Anonymous Type 
Following is example of using Tuple with linq where most of the time we return anonymous type from the method because we just have to select only require number of property from set of property object have.
public static IEnumerable<Tuple<int, string, int>> ProcessListOfDataAndReturn(List<Tuple<int, String>> lst)
{
     Console.WriteLine("ProcessListOfDataAndReturn");
     var data = from tup in lst
                select new Tuple<int, string, int>
                    (
                        tup.Item1,
                        tup.Item2,
                        tup.Item2.Length
                     );
     return data.ToList();
}
You cannot return anonymous type from the method which is one of the constrain. There are other ways to return it from the method which is already discuss by me here Return Anonymous type.
But with the help of Tuple we can easily return Anonymous type data which you can see in above code.
Conclusion
This inbuilt Tuple class in C#4.0 can be use to avoid issue discuss above and also to simplify code at some point
Full Source code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TupleTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("How to crate and use");
            Tuple<int, string> tuple = new Tuple<int, string>(1, "pranay");
            Tuple<int, string> tuple1 = Tuple.Create(2, "ab1");
            Console.WriteLine(tuple.Item1 + "-" + tuple.Item2);
            Console.WriteLine(tuple1.Item1 + "-" + tuple1.Item2);
            Console.WriteLine();
         
            ProcessData(new Tuple<int, string>(1, "Pranay"));

            List<Tuple<int, String>> lst = new List<Tuple<int, string>>();
            lst.Add(new Tuple<int, string>(1, "Pranay"));
            lst.Add(Tuple.Create(2, "ab1"));
            lst.Add(Tuple.Create(1, "abcdef"));
            lst.Add(Tuple.Create(3, "cd2"));
            ProcessListOfData(lst);

            Console.WriteLine("ReturnTupleData");
            var data = ReturnTupleData();
            Console.WriteLine(data.Item1 + "-" + data.Item2);
            Console.WriteLine();

            foreach (var tup in ProcessListOfDataAndReturn(lst))
            {
                Console.WriteLine(tup.Item1 + "-" + tup.Item2+"-" + tup.Item3);
            }
            Console.WriteLine();

            Console.ReadLine();
        }

        public static void ProcessData(Tuple<int, string> tup)
        {
            Console.WriteLine("ProcessData");
            Console.WriteLine(tup.Item1 + "-" + tup.Item2);
            Console.WriteLine();
        }
        public static void ProcessListOfData(List<Tuple<int, String>> lst)
        {
            Console.WriteLine("ProcessListOfData");
            var data = lst.Where(x => x.Item1 == 1).Select(x => x);
            foreach (var tup in data)
            {
                Console.WriteLine(tup.Item1 + "-" + tup.Item2);
            }
            Console.WriteLine();
        }

        public static Tuple<int, string> ReturnTupleData()
        {
            return new Tuple<int, string>(1, "pranay");
        }

        public static IEnumerable<Tuple<int, string, int>> ProcessListOfDataAndReturn(List<Tuple<int, String>> lst)
        {
            Console.WriteLine("ProcessListOfDataAndReturn");
            var data = from tup in lst
                       select new Tuple<int, string, int>
                       (
                           tup.Item1,
                           tup.Item2,
                           tup.Item2.Length
                       );
            return data.ToList();
        }
    }
}

Tuple Object (computer science) Anonymous type Data (computing) Element

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Kotlin Is More Fun Than Java And This Is a Big Deal
  • Understanding gRPC Concepts, Use Cases, and Best Practices
  • Stream Processing vs. Batch Processing: What to Know
  • 2023 Software Testing Trends: A Look Ahead at the Industry's Future

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: