如何在C#中使用字符串和int项创建元组?
首先,在元组中设置两个项目。
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);
}以下是使用字符串和int项目创建元组的示例。
示例
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