C# 元组

C# 中的元组允许我们存储不同数据类型的元素。例如,

var student = ("Taylor", 27, "Orlando");

在这里,student 是一个元组,它包含两个字符串元素("Taylor""Orlando")和一个整数元素(27)。


在 C# 中创建元组

C# 提供了多种创建元组的方法。我们将学习以下方法

  1. 使用括号
  2. 使用 Create() 函数

让我们详细了解这两种方法。


1. 使用括号创建 C# 元组

我们可以通过直接使用括号 () 分配不同的值来创建元组。例如,

using System;

class Program {
    public static void Main() {
// create a tuple containing 3 elements var student= ("Taylor", 5, "Orlando");
Console.WriteLine(student); } }

输出

("Taylor", 5, "Orlando")

在上面的示例中,我们创建了一个元组,而没有指定数据类型。

这样,我们就可以在不指定数据类型的情况下,在元组中存储不同数据类型的元素。

注意: 然而,我们也可以像这样指定元组的数据类型:

using System;

class Program {
	public static void Main() {
	    
(string,int,string) student = ("Taylor", 5, "Orlando");
Console.WriteLine(student); } } // Output: (Taylor, 5, Orlando)

2. 使用 Create() 方法创建 C# 元组

在 C# 中,我们还可以使用 Create() 方法创建元组,而无需指定元组元素的 Dtype。

使用 Create() 创建元组的语法是:

var t1 = Tuple.Create(value);

为了更清楚地理解,让我们看一个例子:

using System;
					
class Program {
    public static void Main() {
// create a tuple containing 3 elements var programming = Tuple.Create("programiz", "java", 12);
Console.WriteLine(programming); } }

输出

(programiz, java, 12)

在上面的示例中,我们使用 Create() 方法创建了一个名为 programming 的元组,其中包含 3 个元素。

注意: 使用 Create() 方法创建元组时,最多只能包含八个元素。


访问元组元素

在前面的示例中,我们直接显示了整个元组。但是,可以访问元组的每个元素。

在 C# 中,元组的每个元素都关联有一个默认名称。

  • 第一个元素 - Item1
  • 第二个元素 - Item2
  • 依此类推

我们可以使用默认名称访问元组的元素。例如,

using System;
					
class Program {
	public static void Main() {
	    
	    var subjects = Tuple.Create("Science", "Maths", "English");
// access the first element Console.WriteLine("The first element is " + subjects.Item1);
// access the second element Console.WriteLine("The second element is " +subjects.Item2); } }

输出

The first element is Science
The second element is Maths

在上面的示例中

  • subjects.Item1 - 访问第一个元素
  • subjects.Item2 - 访问第二个元素

更改元组元素的值

我们可以更改元组中的数据值。要更改元组的元素,我们可以将一个新值重新分配给默认名称。例如,

using System;
					
class Program {
    public static void Main() {
        var roll_num = (5, 7, 8, 3);
	    
         // original tuple
         Console.WriteLine("Original Tuple: " + roll_num);
	    
        // replacing the value 7 with 6
        roll_num.Item2 = 6; 
         Console.WriteLine("Changing value of 2nd element to " + roll_num.Item2);
         Console.WriteLine("Modified Tuple: " + roll_num);
    }
}

输出

Original Tuple: (5, 7, 8, 3)
Changing value of 2nd element to 6
Modified Tuple: (5, 6, 8, 3)

在上面的示例中,我们将第二个元素的值替换为 6

注意: 如果我们使用 Create() 创建了元组,那么我们就不能更改元素的值。这意味着元组的元素是只读的。

例如,

using System;

class Program {
	public static void Main() {
	    var t1= Tuple.Create("Taylor", "Jack");
t1.Item2 = "Monica";
Console.WriteLine(t1.Item2); } }

输出

Property or indexer 'Tuple<string, string>.Item2' cannot be assigned to -- it is read only

嵌套元组

我们可以在另一个元组中创建一个元组。嵌套在元组中的元组称为嵌套元组。例如,

using System;
					
class Program {
    public static void Main() {
	    
var myTuple= Tuple.Create("Taylor", "Jack", Tuple.Create(7, 8, 9));
Console.WriteLine("The elements inside myTuple: " + myTuple); } }

输出

The elements inside myTuple: (Taylor, Jack, (7, 8, 9))   

在这里,我们有一个元组 (7, 8, 9) 嵌套在 myTuple 元组中。这称为嵌套元组。


C# 元组中的其他主题

C# 元组作为方法参数

在 C# 中,我们也可以将元组作为参数传递给另一个方法。例如,

using System;
					
class Program {

    // method
static void displayTuple(Tuple<int, string, int>numbers) {
Console.WriteLine("Tuple: " + numbers); } public static void Main() { // create a tuple var numbers = Tuple.Create(1, "John", 2);
// passing the tuple inside method as a parameter displayTuple(numbers);
} }

输出

Tuple: (1, John, 2)

在这里,我们将 numbers 元组作为参数传递给了 displayTuple() 方法。

注意代码

static void displayTuple(Tuple<int, string, int>numbers) {
        Console.WriteLine("Tuple: " + numbers);
    }

这就是如何将元组作为参数传递。

C# 元组作为返回类型

我们也可以在方法中返回一个元组。例如,

using System;
					
class Program {
    public static void Main() {
	    
         // the return value of displaySalary() is stored in employee tuple
        var employee = displaySalary();
	    
        // accessing the second element of employee  
        Console.WriteLine(employee.Item2);
	    
    } 
// the return type of displaySalary() method is a tuple containing three elements static Tuple<string, int, int> displaySalary() { return Tuple.Create("Selena", 15000, 23);
} }

输出

The first element of tuple: 1

在上面的示例中,displaySalary() 方法返回一个包含三个元素的元组:"Selena"1500023

元组中的命名属性

我们可以像这样为元组中的字段/属性命名:

var t1= (Subject: "Math", Grade: 2);

这里,SubjectGrade 分别是元素 "Math"2 的名称。

我们可以使用它们的名称访问这些元素。例如,

using System;
					
class Program {
    public static void Main() {
var t1 = (Subject: "Math", Grade: 2);
// accessing the element of t1 using their names Console.WriteLine(t1.Subject); Console.WriteLine(t1.Grade); } }

输出

Math
2
为什么要使用元组?

元组提供了一种方便的方式来存储多种数据类型的元素。

单独声明每个数据既耗时。我们可以使用元组来代替为各个元素创建单独的数据结构。

你觉得这篇文章有帮助吗?

我们的高级学习平台,凭借十多年的经验和数千条反馈创建。

以前所未有的方式学习和提高您的编程技能。

试用 Programiz PRO
  • 交互式课程
  • 证书
  • AI 帮助
  • 2000+ 挑战