C#元组解析和用法详细指南

2021年3月9日15:58:29 发表评论 624 次浏览

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

元组的特征:

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

C#元组有什么需要?

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

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

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

创建一个元组

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

使用元组类的构造函数:

你可以使用提供的构造函数创建一个元组

元组<T>类

。你可以在其中存储类型从1到8的元素。但是, 不允许在一个元组中存储大于8的元素。如果尝试这样做, 则编译器将引发错误。

语法如下:

// Constructor for single elements
Tuple <T1>(T1)

// Constructor for two elements
Tuple <T1, T2>(T1, T2)
.
.
.
 // Constructor for eight elements
Tuple <T1, T2, T3, T4, T5, T6, T7, TRest>(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
     Tuple< string >My_Tuple1 = new Tuple< string >( "lsbin" );
       
     // Tuple with three elements
     Tuple< string , string , int >My_Tuple2 = new Tuple< string , string , int >( "Romil" , "Python" , 29);
       
     // Tuple with eight elements
     Tuple< int , int , int , int , int , int , int , Tuple< int >>My_Tuple3 = new Tuple< int , int , int , int , int , int , int , Tuple< int >>(1, 2, 3, 4, 5, 6, 7, new Tuple< int >(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( "lsbin" );
  
         // 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>属性, 这里elementNumber是1到7之间的数字, 例如Item1, Item 2, Item3, Item4, Item5, Item6, Item 7等。通过使用Rest属性可以访问8元组的最后一个元素。如下例所示:

例子:

// 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( "lsbin" );
  
         // 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: lsbin

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 <elementNumber>属性访问该元组, 如示例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()方法和元组的元素是通过使用Item <elementNumber>属性访问的。

例子:

// 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( "lsbin" , 123, 90.8);
          
         // Pass the tuple in the
                 // PrintTheTuple method
         PrintTheTuple(mytuple);
     }
  
     static void PrintTheTuple(Tuple< string , int , double >mytuple)
         {
         Console.WriteLine( "Element: " +mytuple.Item1);
         Console.WriteLine( "Element: " +mytuple.Item2);
         Console.WriteLine( "Element: " +mytuple.Item3);
     }
}

输出如下:

Element: lsbin
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 Tuple< string , string , string >PrintTuple()
         {
         return Tuple.Create( "Geeks" , "For" , "Geeks" );
     }
}

输出如下:

Geeks
For
Geeks

元组的局限性:

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

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: