如何在 C# 中建立 1 元組或單元素元組?


Tuple<T1> 類表示一個 1 元組,稱為單元素元組。元組是一個包含一系列元素的資料結構。

示例

現在,讓我們看一個示例來實現 C# 中的 1 元組 −

using System;
public class Demo {
   public static void Main(string[] args) {
      Tuple<int> tuple = new Tuple<int>(100);
      Console.WriteLine("Value = " + tuple.Item1);
      if (tuple.Item1 == 150) {
         Console.WriteLine(tuple.Item1);
      }
      if (tuple.Item1 == 100) {
         Console.WriteLine(tuple.Item1);
      }
   }
}

輸出

這將生成以下輸出 −

Value = 100
100

示例

現在,讓我們看另一個示例來實現 C# 中的 1 元組 −

using System;
public class Demo {
   public static void Main(string[] args) {
      Tuple<string> tuple = new Tuple<string>("amit");
      Console.WriteLine("Value = " + tuple.Item1);
      if (tuple.Item1 == "tom") {
         Console.WriteLine(tuple.Item1);
      }
      if (tuple.Item1 == "amit") {
         Console.WriteLine("Exists: Tuple Value = " +tuple.Item1);
      }
   }
}

輸出

這將生成以下輸出 −

Value = amit
Exists: Tuple Value = amit

更新時間:2019 年 11 月 6 日

109 次瀏覽

開啟你的 職業生涯

完成該課程即可獲得認證

開始
廣告