如何在C#中创建2元组或成对元组?
Tuple <T1,T2>类表示2元组,称为对。元组是具有一系列元素的数据结构。
示例
现在让我们看一个在C#中实现2元组的示例-
using System;public class Demo {
public static void Main(string[] args) {
Tuple<string,string> tuple = new Tuple<string,string>("jack", "steve");
Console.WriteLine("Value = " + tuple.Item1);
if (tuple.Item1 == "jack") {
Console.WriteLine("Exists: Tuple Value = " +tuple.Item1);
}
if (tuple.Item2 == "david") {
Console.WriteLine("Exists: Tuple Value = " +tuple.Item2);
}
}
}
输出结果
这将产生以下输出-
Value = jackExists: Tuple Value = jack
示例
现在让我们来看另一个示例,以在C#中实现2元组-
using System;public class Demo {
public static void Main(string[] args) {
Tuple tuple = new Tuple(20, "steve");
Console.WriteLine("Value = " + tuple.Item1);
if (tuple.Item1 == 20) {
Console.WriteLine("Exists: Tuple Value = " +tuple.Item1);
}
if (tuple.Item2 == "david") {
Console.WriteLine("Exists: Tuple Value = " +tuple.Item2);
}
}
}
输出结果
这将产生以下输出-
Value = 20Exists: Tuple Value = 20
以上是 如何在C#中创建2元组或成对元组? 的全部内容, 来源链接: utcz.com/z/335318.html