如何在 C# 中建立一個包含字串和整數項的元組?


首先,在元組中設定兩個項。

Tuple<int, string> tuple = new Tuple<int, string>(20, "Tom");

現在檢查元組中的第一個項,這是一個整數。

if (tuple.Item1 == 20) {
   Console.WriteLine(tuple.Item1);
}

現在檢查元組中的第二個項,它是一個字串 -

if (tuple.Item2 == "Tom") {
   Console.WriteLine(tuple.Item2);
}

以下是如何建立一個包含字串和整數項的元組的示例。

示例

 即時演示

using System;
using System.Threading;
namespace Demo {
   class Program {
      static void Main(string[] args) {
         Tuple<int, string> tuple = new Tuple<int, string>(20, "Tom");
         if (tuple.Item1 == 20) {
            Console.WriteLine(tuple.Item1);
         }
         if (tuple.Item2 == "Tom") {
            Console.WriteLine(tuple.Item2);
         }
      }
   }
}

輸出

20
Tom

更新於: 23-6 月-2020

325 次檢視

啟動您的 事業

透過完成課程獲得認證

開始
廣告