How to create 2-tuple or pair tuple in C#?
In C#, a 2-tuple is a tuple that contains two elements and it is also known as pair. You can create a 2-tuple using two different ways:
Using Tuple<T1,T2>(T1, T2) Constructor
You can create 2-tuple using Tuple<T1, T2>(T1, T2) constructor. It initializes a new instance of the Tuple<T1,T2> class. But when you create a tuple using this constructor then you have to specify the type of the element stored in the tuple.
Syntax:
public Tuple (T1 item1, T2 item2);
Parameters:
- item1: It is the value of the first tuple component.
- item2: It is the value of the second tuple component.
Example:
// C# program to create 2-tuple // using the tuple constructor using System; public class GFG { // Main method static public void Main() { // Creating tuple with two elements // Using Tuple<P1, P2>(T1, T2) constructor Tuple< string , int > My_Tuple = new Tuple< string , int >( "GeeksforGeeks" , 10); Console.WriteLine( "Element 1: " + My_Tuple.Item1); Console.WriteLine( "Element 2: " + My_Tuple.Item2); } } |
Element 1: GeeksforGeeks Element 2: 10
Using Create Method
You can also create 2-tuple with the help of Create method. When you use this method then there is no need to specify the type of the elements stored in the tuple.
Syntax:
public static Tuple<T1,T2> Create<T1, T2> (T1 item1, T2 item2);
Type Parameters:
- T1: It is the type of the first tuple component.
- T2: It is the type of the second tuple component.
Parameters:
- item1: It is the value of the first tuple component.
- item2: It is the value of the second tuple component.
Return Type: This method returns 2-tuple whose value is item1 and item2.
Example:
// C# program to create 2-tuple // using create method using System; public class GFG { // Main method static public void Main() { // Creating tuple with two elements // Using Create method var My_Tuple = Tuple.Create( "Geeks" , 20); Console.WriteLine( "Element 1: " + My_Tuple.Item1); Console.WriteLine( "Element 2: " + My_Tuple.Item2); } } |
Element 1: Geeks Element 2: 20
References:
- https://docs.microsoft.com/en-us/dotnet/api/system.tuple-2.-ctor?view=netframework-4.8
- https://docs.microsoft.com/en-us/dotnet/api/system.tuple.create?view=netframework-4.8#System_Tuple_Create__2___0___1_
Recommended Posts:
- How to create 1-Tuple or Singleton Tuple in C#?
- How to create 3-Tuple or Triple Tuple in C#?
- How to create 6-Tuple in C#?
- How to create 8-Tuple or Octuple in C#?
- How to create 4-Tuple or quadruple in C#?
- How to create 5-Tuple or quintuple in C#?
- How to create 7-Tuple or Septuple in C#?
- C# | Tuple
- C# | How to get Fifth Element of the Tuple?
- C# | How to get First Element of the Tuple?
- C# | Tuple<T1,T2,T3,T4,T5,T6,T7> Class
- C# | How to get Second Element of the Tuple?
- C# | How to get Third Element of the Tuple?
- C# | How to get the HashCode of the tuple?
- C# | Tuple Class
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.