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
Partner Zones AWS Cloud
by AWS Developer Relations
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
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report

The New Tuple Feature in C# 4.0

Jalpesh Vadgama user avatar by
Jalpesh Vadgama
·
Jun. 09, 12 · Interview
Like (0)
Save
Tweet
Share
7.51K Views

Join the DZone community and get the full member experience.

Join For Free
The new version of C# (4.0) includes a new feature called Tuples. Tuples provides a way of grouping elements of different data types. That enables us to use it a lot where we can store coordinates of graphs etc.

In C# 4.0 we can create a Tuple with the Create method. This Create method offers 8 overloads like the following. So you can group a maximum of 8 data types with a Tuple. Followings are overloads of a data type.

  • Create(T1)- Which represents a tuple of size 1
  • Create(T1,T2)- Which represents a tuple of size 2
  • Create(T1,T2,T3) – Which represents a tuple of size 3
  • Create(T1,T2,T3,T4) – Which represents a tuple of size 4
  • Create(T1,T2,T3,T4,T5) – Which represents a tuple of size 5
  • Create(T1,T2,T3,T4,T5,T6) – Which represents a tuple of size 6
  • Create(T1,T2,T3,T4,T5,T6,T7) – Which represents a tuple of size 7
  • Create(T1,T2,T3,T4,T5,T6,T7,T8) – Which represents a tuple of size 8
Following are some example code snippets for tuple.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace TupleExample
{
class Program
{
 static void Main(string[] args)
 {
     var tuple = System.Tuple.Create<string, string, string>("Jalpesh", "P", "Vadgama");
     Console.WriteLine(tuple);
 
     var t = System.Tuple.Create<int, string>(1, "Jalpesh");
     Console.WriteLine(t);
 
 }
}
}
Following is a output of above as expected.

Tuple in C# 4.0

You can also access values inside Tuples with ItemN property. Where N represents a particular item in the tuple. The following is an example of it.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace TupleExample
{
class Program
{
 static void Main(string[] args)
 {
     var tuple = System.Tuple.Create<string, string, string>("Jalpesh", "P", "Vadgama");
     Console.WriteLine(tuple.Item1);
     Console.WriteLine(tuple.Item2);
     Console.WriteLine(tuple.Item3);
 }
}
}
Here you can see I have printed items with Item1,Item2 and Item3 . Following is the output of above code.

Tuple example in C# 4.0

We can also create a nested tuple.  The following is code for a nested tuple.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace TupleExample
{
class Program
{
 static void Main(string[] args)
 {
     var tuple = System.Tuple.Create(1,"Jalpesh",new Tuple<string,string>("P","Vadgama"));
     Console.WriteLine(tuple.Item1);
     Console.WriteLine(tuple.Item2);
     Console.WriteLine(tuple.Item3);
 
 }
}
}
The following is output of above code as expected.

Netsted tuple in C# 4.0

As you can see there are unlimited possibilities. We can do lots of things with Tuples. Hope you liked it. Stay tuned for more. Till then, Happy Programming!!

Tuple

Published at DZone with permission of Jalpesh Vadgama, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • The Power of Docker Images: A Comprehensive Guide to Building From Scratch
  • Distributed Tracing: A Full Guide
  • A Beginner’s Guide To Styling CSS Forms
  • Configure Kubernetes Health Checks

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: