首先,声明一个元组并添加值-
Tuple <int, string> tuple = new Tuple<int, string>(100, "Tom");
使用C#,要遍历元组,您可以使用单个元素-
tuple.Item1 // first item tuple.Item2 // second item To display the complete tuple, just use: //显示整个元组 Console.WriteLine(tuple);
让我们看完整的代码-
using System; using System.Threading; namespace Demo { class Program { static void Main(string[] args) { Tuple <int, string> tuple = new Tuple<int, string>(100, "Tom"); if (tuple.Item1 == 100) { Console.WriteLine(tuple.Item1); } if (tuple.Item2 == "Tom") { Console.WriteLine(tuple.Item2); } //显示整个元组 Console.WriteLine(tuple); } } }