C# | Getting the Type of the Tuple’s Element
A tuple is a data structure which gives you the easiest way to represent a data set. You can get the type of the tuple objects by using the GetType method. This method will return the type of the tuple objects. This method is inherited from the Object Class.
Syntax:
public Type GetType ();
Return Type: The return type of this method is Type. Means it return the type of the Tuple<> object, where Tuple<> is may be 1-tuple, or 2-tuple, or 3-tuple, or 4-tuple, or 5-tuple, or 6-tuple, or 7-tuple, or 8-tuple.
Below programs illustrate the use of the above-discuss method:
Example 1:
// C# program to illustrate the // use of GetType method using System; class GFG { // Main Method static public void Main() { // Taking 1-Tuple var v1 = Tuple.Create( "Beginner" ); Console.WriteLine( "Type of the element of 1-Tuple: " + v1.Item1.GetType()); Console.WriteLine(); // Taking 2-Tuple var v2 = Tuple.Create( "w3wiki" , 23); Console.WriteLine( "Type of the First Element of 2-Tuple: " + v2.Item1.GetType()); Console.WriteLine( "Type of the Second Element of 2-Tuple: " + v2.Item2.GetType()); Console.WriteLine(); // Taking 3-Tuple var v3 = Tuple.Create( "Beginner" , 234, 90.9); Console.WriteLine( "Type of the First element of 3-Tuple: " + v3.Item1.GetType()); Console.WriteLine( "Type of the Second element of 3-Tuple: " + v3.Item2.GetType()); Console.WriteLine( "Type of the Third element of 3-Tuple: " + v3.Item3.GetType()); Console.WriteLine(); // Taking 4-Tuple var v4 = Tuple.Create( "Beginner" , 567, 56.7, 'g' ); Console.WriteLine( "Type of the First element of 3-Tuple: " + v4.Item1.GetType()); Console.WriteLine( "Type of the Second element of 3-Tuple: " + v4.Item2.GetType()); Console.WriteLine( "Type of the Third element of 3-Tuple: " + v4.Item3.GetType()); Console.WriteLine( "Type of the Fourth element of 3-Tuple: " + v4.Item4.GetType()); // Taking 7-Tuple var v7 = Tuple.Create( "Beginner" , 234.0, 90.9f, 56.6m, 67.8, true , 67.8); Console.WriteLine( "Type of the First element of 7-Tuple: " + v7.Item1.GetType()); Console.WriteLine( "Type of the Second element of 7-Tuple: " + v7.Item2.GetType()); Console.WriteLine( "Type of the Third element of 7-Tuple: " + v7.Item3.GetType()); Console.WriteLine( "Type of the Fourth element of 7-Tuple: " + v7.Item4.GetType()); Console.WriteLine( "Type of the Fifth element of 7-Tuple: " + v7.Item5.GetType()); Console.WriteLine( "Type of the Sixth element of 7-Tuple: " + v7.Item6.GetType()); Console.WriteLine( "Type of the Seventh element of 7-Tuple: " + v7.Item7.GetType()); Console.WriteLine(); } } |
Output:
Type of the element of 1-Tuple: System.String Type of the First Element of 2-Tuple: System.String Type of the Second Element of 2-Tuple: System.Int32 Type of the First element of 3-Tuple: System.String Type of the Second element of 3-Tuple: System.Int32 Type of the Third element of 3-Tuple: System.Double Type of the First element of 3-Tuple: System.String Type of the Second element of 3-Tuple: System.Int32 Type of the Third element of 3-Tuple: System.Double Type of the Fourth element of 3-Tuple: System.Char Type of the First element of 7-Tuple: System.String Type of the Second element of 7-Tuple: System.Double Type of the Third element of 7-Tuple: System.Single Type of the Fourth element of 7-Tuple: System.Decimal Type of the Fifth element of 7-Tuple: System.Double Type of the Sixth element of 7-Tuple: System.Boolean Type of the Seventh element of 7-Tuple: System.Double
Contact Us