如何在 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
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP