📜  C#|元组

📅  最后修改于: 2021-05-29 21:12:06             🧑  作者: Mango

元组一词的意思是“由多个部分组成的数据结构”。因此,元组是一种数据结构,它为您提供最简单的方式来表示一个数据集,该数据集具有多个可能彼此相关的值。它在.NET Framework 4.0中引入。在元组中,您可以添加1到8的元素。如果尝试添加大于八个的元素,则编译器将引发错误。当您要创建一个包含对象及其属性的数据结构,而又不想为其创建单独的类型时,通常会使用元组。

元组的特征:

  • 它使我们可以将多个数据表示为一个数据集。
  • 它使我们能够创建,操作和访问数据集。
  • 它从方法返回多个值,而不使用out参数。
  • 它还可以存储重复的元素。
  • 它允许我们借助单个参数将多个值传递给方法。

C#元组有什么需求?

在元组之前,我们有三种方法可以从C#中的方法返回多个值,即使用类或结构类型,输出参数通过动态返回类型返回的匿名类型。但是在使用元组之后,可以很容易地表示一组数据。

出于另一个原因,试想一下您想在一个实体(例如姓名,EmpID,血型,联系电话)中存储员工详细信息的情况。现在想到的最常见的方法是创建一个将包含必填字段的数据结构。这就是Tuples发挥作用的地方。使用元组,无需创建任何单独的数据结构。相反,对于这种情况,可以使用Tuple

最常见的数据结构(例如Array,List等)仅具有特定类型,并且可以存储无限个元素。但是元组只能存储有限数量的元素(即8),并且可以是任何类型。

创建一个元组

在C#中,主要有两种创建元组的方法,如下所示:

  • 使用Tuple类的构造函数:您可以使用Tuple 提供的构造函数来创建一个元组。您可以在其中存储类型从1到8的元素。但是,不允许在一个元组中存储大于8的元素。如果尝试这样做,则编译器将引发错误。

    句法:

    // Constructor for single elements
    Tuple (T1)
    
    // Constructor for two elements
    Tuple (T1, T2)
    .
    .
    .
     // Constructor for eight elements
    Tuple (T1, T2, T3, T4, T5, T6, T7, TRest)

    例子:

    // C# program to create tuple using tuple constructor.
    using System;
       
    public class GFG{
           
        // Main method
        static public void Main (){
               
            // Tuple with one element
        TupleMy_Tuple1 = new Tuple("GeeksforGeeks");
           
        // Tuple with three elements
        TupleMy_Tuple2 = new Tuple("Romil", "Python", 29);
           
        // Tuple with eight elements
        Tuple>My_Tuple3 = new Tuple>(1,2,3,4,5,6,7, new Tuple(8));
        }
    }
    
  • 使用创建方法:当我们使用元组构造函数创建元组时,我们需要提供存储在元组中的每个元素的类型,这会使您的代码繁琐。因此,C#提供了另一个名为Tuple的类,该类包含用于创建tuple对象的静态方法,而无需提供每个元素的类型。

    句法:

    // Method for 1-tuple
    Create(T1)
    
    // Method for 2-tuple
    Create(T1, T2)
    .
    .
    .
    // Method for 8-tuple
    Create(T1, T2, T3, T4, T5, T6, T7, T8)
    

    例子:

    // C# program to create tuple 
    // using Create Method
    using System;
      
    public class GFG {
      
        // Main method
        static public void Main()
        {
      
            // Creating 1-tuple
            // Using Create Method
            var My_Tuple1 = Tuple.Create("GeeksforGeeks");
      
            // Creating 4-tuple
            // Using Create Method
            var My_Tuple2 = Tuple.Create(12, 30, 40, 50);
      
            // Creating 8-tuple
            // Using Create Method
            var My_Tuple3 = Tuple.Create(13, "Geeks", 67, 
                          89.90, 'g', 39939, "geek", 10);
        }
    }
    

    访问元组

    您可以使用Item 属性访问元组的元素,这里elementNumber是1到7,例如Item1,Item 2,Item3,Item4,Item5,Item6,Item 7等,以及8元组的最后一个元素可通过使用Rest属性进行访问。如下例所示:

    例子:

    // C# program to access the tuple 
    // using Item and Rest property
    using System;
      
    public class GFG {
      
        // Main method
        static public void Main()
        {
      
            // Creating 1-tuple
            // Using Create Method
            var My_Tuple1 = Tuple.Create("GeeksforGeeks");
      
            // Accessing the element of Tuple
            // Using Item property
            Console.WriteLine("Element of My_Tuple1: " + My_Tuple1.Item1);
            Console.WriteLine();
      
            // Creating 4-tuple
            // Using Create Method
            var My_Tuple2 = Tuple.Create(12, 30, 40, 50);
      
            // Accessing the element of Tuple
            // Using Item property
            Console.WriteLine("Element of My_Tuple2: " + My_Tuple2.Item1);
            Console.WriteLine("Element of My_Tuple2: " + My_Tuple2.Item2);
            Console.WriteLine("Element of My_Tuple2: " + My_Tuple2.Item3);
            Console.WriteLine("Element of My_Tuple2: " + My_Tuple2.Item4);
            Console.WriteLine();
      
            // Creating 8-tuple
            // Using Create Method
            var My_Tuple3 = Tuple.Create(13, "Geeks",
                  67, 89.90, 'g', 39939, "geek", 10);
      
            // Accessing the element of Tuple
            // Using Item property
            // And print the 8th element of tuple
            // using Rest property
            Console.WriteLine("Element of My_Tuple3: " + My_Tuple3.Item1);
            Console.WriteLine("Element of My_Tuple3: " + My_Tuple3.Item2);
            Console.WriteLine("Element of My_Tuple3: " + My_Tuple3.Item3);
            Console.WriteLine("Element of My_Tuple3: " + My_Tuple3.Item4);
            Console.WriteLine("Element of My_Tuple3: " + My_Tuple3.Item5);
            Console.WriteLine("Element of My_Tuple3: " + My_Tuple3.Item6);
            Console.WriteLine("Element of My_Tuple3: " + My_Tuple3.Item7);
            Console.WriteLine("Element of My_Tuple3: " + My_Tuple3.Rest);
        }
    }
    

    输出:

    Element of My_Tuple1: GeeksforGeeks
    
    Element of My_Tuple2: 12
    Element of My_Tuple2: 30
    Element of My_Tuple2: 40
    Element of My_Tuple2: 50
    
    Element of My_Tuple3: 13
    Element of My_Tuple3: Geeks
    Element of My_Tuple3: 67
    Element of My_Tuple3: 89.9
    Element of My_Tuple3: g
    Element of My_Tuple3: 39939
    Element of My_Tuple3: geek
    Element of My_Tuple3: (10)
    

    嵌套元组

    在C#中,允许您将一个元组创建到另一个元组中。当您想在同一元组中添加八个以上的元素时,可以使用嵌套元组。可以通过使用Rest属性来访问嵌套元组,如示例1所示。您可以在序列中的任何位置添加嵌套元组,但是建议您将嵌套元组放置在序列的末尾,以便轻松地进行操作。从Rest属性访问。如果将嵌套元组放置在最后一个位置之外,则可以根据Item 属性访问该元组,如示例2所示。

    范例1:

    // C# program to illustrate nested tuple
    using System;
      
    public class GFG{
          
            // Main method
        static public void Main ()
            {
              
               // Nested Tuple
            var My_Tuple = Tuple.Create(13, "Geeks", 67, 89.90,
                     'g', 39939, "geek", Tuple.Create(12, 30, 40, 50));
              
            // Accessing the element of Tuple
            // Using Item property
            // And accessing the elements of nested tuple
            // Using Rest property
            Console.WriteLine("Element of My_Tuple: "+My_Tuple.Item1);
            Console.WriteLine("Element of My_Tuple: "+My_Tuple.Item2);
            Console.WriteLine("Element of My_Tuple: "+My_Tuple.Item3);
            Console.WriteLine("Element of My_Tuple: "+My_Tuple.Item4);
            Console.WriteLine("Element of My_Tuple: "+My_Tuple.Item5);
            Console.WriteLine("Element of My_Tuple: "+My_Tuple.Item6);
            Console.WriteLine("Element of My_Tuple: "+My_Tuple.Item7);
            Console.WriteLine("Element of Nested tuple: "+My_Tuple.Rest);
            Console.WriteLine("Element of Nested tuple: "+My_Tuple.Rest.Item1.Item1);
            Console.WriteLine("Element of Nested tuple: "+My_Tuple.Rest.Item1.Item2);
            Console.WriteLine("Element of Nested tuple: "+My_Tuple.Rest.Item1.Item3);
            Console.WriteLine("Element of Nested tuple: "+My_Tuple.Rest.Item1.Item4);
        }
    }
    

    输出:

    Element of My_Tuple: 13
    Element of My_Tuple: Geeks
    Element of My_Tuple: 67
    Element of My_Tuple: 89.9
    Element of My_Tuple: g 
    Element of My_Tuple: 39939
    Element of My_Tuple: geek
    Element of Nested tuple: ((12, 30, 40, 50))
    Element of Nested tuple: 12
    Element of Nested tuple: 30
    Element of Nested tuple: 40
    Element of Nested tuple: 50
    

    范例2:

    // C# program to illustrate nested tuple
    using System;
      
    public class GFG{
          
            // Main method
        static public void Main ()
            {
              
               // Nested Tuple
               // Here nested tuple is present 
                   // at the place of 2nd element
            var My_Tuple = Tuple.Create(13, Tuple.Create(12, 30, 40,
                                   50),67, 89.90, 'g', 39939, 123, "geeks");
              
            // Accessing the element of Tuple
            // Using Item property
            // And accessing the elements of 
            // nested tuple Using Rest property
            Console.WriteLine("Element of My_Tuple: "+My_Tuple.Item1);
            Console.WriteLine("Element of Nested Tuple: "+My_Tuple.Item2.Item1);
            Console.WriteLine("Element of Nested Tuple: "+My_Tuple.Item2.Item2);
            Console.WriteLine("Element of Nested Tuple: "+My_Tuple.Item2.Item3);
            Console.WriteLine("Element of Nested Tuple: "+My_Tuple.Item2.Item4);
            Console.WriteLine("Element of My_Tuple: "+My_Tuple.Item3);
            Console.WriteLine("Element of My_Tuple: "+My_Tuple.Item4);
            Console.WriteLine("Element of My_Tuple: "+My_Tuple.Item5);
            Console.WriteLine("Element of My_Tuple: "+My_Tuple.Item6);
            Console.WriteLine("Element of My_Tuple: "+My_Tuple.Item7);
            Console.WriteLine("Element of My_Tuple: "+My_Tuple.Rest);
              
        }
    }
    

    输出:

    Element of My_Tuple: 13
    Element of Nested Tuple: 12
    Element of Nested Tuple: 30
    Element of Nested Tuple: 40
    Element of Nested Tuple: 50
    Element of My_Tuple: 67
    Element of My_Tuple: 89.9
    Element of My_Tuple: g
    Element of My_Tuple: 39939
    Element of My_Tuple: 123
    Element of My_Tuple: (geeks)

    元组作为方法参数

    在C#中,允许您将元组作为方法参数传递,如以下示例所示。在这里,我们在PrintTheTuple()方法中传递了一个名为mytuple的元组,并且使用Item 属性访问了该元组的元素。

    例子:

    // C# program to illustrate the 
    // tuple as a method parameter.
    using System;
      
    public class GFG{
          
            // Main method
        static public void Main ()
            {
              
                // Creating a tuple 
            var mytuple = Tuple.Create("GeeksforGeeks", 123, 90.8);
              
            // Pass the tuple in the
                    // PrintTheTuple method
            PrintTheTuple(mytuple);
        }
      
        static void PrintTheTuple(Tuplemytuple)
            {
            Console.WriteLine("Element: "+mytuple.Item1);
            Console.WriteLine("Element: "+mytuple.Item2);
            Console.WriteLine("Element: "+mytuple.Item3);
        }
    }
    

    输出:

    Element: GeeksforGeeks
    Element: 123
    Element: 90.8
    

    元组作为返回类型

    在C#中,方法被允许使用元组作为返回类型。换句话说,方法可以返回一个元组,如下面的示例所示:

    例子:

    // C# program to illustrate 
    // how a method return tuple
    using System;
      
    public class GFG{
          
            // Main Method
        static public void Main ()
            {
                // Return tuple is stored in mytuple
            var mytuple = PrintTuple();
            Console.WriteLine(mytuple.Item1);
            Console.WriteLine(mytuple.Item2);
            Console.WriteLine(mytuple.Item3);
        }
          
        // PrintTuple method return a tuple 
        static TuplePrintTuple()
            {
            return Tuple.Create("Geeks", "For", "Geeks");
        }
    }
    

    输出:

    Geeks
    For
    Geeks
    

    元组的局限性:

    • 它是引用类型,而不是值类型。
    • 它限于八个元素。意味着如果没有嵌套元组,则不能存储八个以上的元素。
    • 这些只能通过使用Item 属性访问