ValueTuple es una estructura introducida en C# 7.0 que representa el tipo de valor Tuple. Le permite almacenar un conjunto de datos que contiene múltiples valores que pueden o no estar relacionados entre sí. La propiedad Item4 se utiliza para obtener el cuarto elemento sin nombre de la tupla de valor dada. Es aplicable en cada tupla de valor como 4-ValueTuple, 5-ValueTuple, etc.
Sintaxis:
public T4 Item4;
Aquí, T4 es el valor de campo de una estructura ValueTuple<>. Este ValueTuple<> puede ser 4-ValueTuple, o 5-ValueTuple, o 6-ValueTuple, o 7-ValueTuple, o 8-ValueTuple.
Ejemplo: en el siguiente código, puede ver que estamos accediendo al cuarto elemento de cada tupla de valor.
// C# program to illustrate how to get // the fourth element of value tuple using System; class GFG { // Main Method static public void Main() { Console.WriteLine("C# Topics:"); // Creating a value tuple // with four elements var ValTpl4 = ValueTuple.Create("Stack", "Dictionary", "LinkedList", "Interface"); // Accessing the fourth element of // 4-ValueTuple using Item property Console.WriteLine(ValTpl4.Item4); // Creating a value tuple with five elements var ValTpl5 = ValueTuple.Create("Identifiers", "Data Types", "Keywords", "Access Modifiers", "Operators"); // Accessing the fourth element of // 5-ValueTuple using Item property Console.WriteLine(ValTpl5.Item4); // Creating a value tuple with six elements var ValTpl6 = ValueTuple.Create("Nullable Types", "Class", "Structure", "Indexers", "Switch Statement", "Loops"); // Accessing the fourth element of // 6-ValueTuple using Item property Console.WriteLine(ValTpl6.Item4); // Creating a value tuple with seven elements var ValTpl7 = ValueTuple.Create("Inheritance ", "Constructors", "Encapsulation", "Abstraction", "Static Class", "Partial Classes", "this keyword"); // Accessing the fourth element of // 7-ValueTuple using Item property Console.WriteLine(ValTpl7.Item4); // Creating a value tuple with eight elements var ValTpl8 = ValueTuple.Create("Methods", "Method Hiding", "Optional Parameters", "Anonymous Method", "Partial Methods", "Local Function", "Delegates", "Destructors"); // Accessing the fourth element of // 8-ValueTuple using Item property Console.WriteLine(ValTpl8.Item4); } }
C# Topics: Interface Access Modifiers Indexers Abstraction Anonymous Method
Ejemplo 2:
// C# program to get the hash code of // fifth element in a value tuple using System; class GFG { // Main method static public void Main() { // Creating 5-ValueTuple var My_Value_Tuple = (1004, "Rohit", "Computer Science", 24, "C#"); // Accessing fifth element of the value tuple Console.WriteLine("Favourite programming Language: {0}", My_Value_Tuple.Item5); // Getting the hashcode of the fifth element Console.WriteLine("Hash Code: {0}", My_Value_Tuple.Item5.GetHashCode()); } }
Favourite programming Language: C# Hash Code: -849950870
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