C# | Obtener el tipo del elemento de la tupla

Una tupla es una estructura de datos que le brinda la forma más fácil de representar un conjunto de datos. Puede obtener el tipo de los objetos de tupla utilizando el método GetType . Este método devolverá el tipo de los objetos de tupla. Este método se hereda de la clase de objeto .

Sintaxis:

public Type GetType ();

Tipo de devolución: el tipo de devolución de este método es Type . Significa que devuelve el tipo del objeto Tuple<>, donde Tuple<> puede ser 1-tupla, o 2-tupla, o 3-tupla, o 4-tupla, o 5-tupla, o 6-tupla, o 7 -tupla u 8-tupla.

Los siguientes programas ilustran el uso del método mencionado anteriormente:

Ejemplo 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("Geeks");
        Console.WriteLine("Type of the element of 1-Tuple: " + v1.Item1.GetType());
        Console.WriteLine();
  
        // Taking 2-Tuple
        var v2 = Tuple.Create("GeeksforGeeks", 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("geeks", 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("geeks", 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("geeks", 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();
  
    }
}
Producción:

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

Publicación traducida automáticamente

Artículo escrito por ankita_saini y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *