如何在 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