如何对 C# 元组进行迭代?
首先声明一个元组并添加值 −
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: // display entire tuple 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); } // display entire tuple Console.WriteLine(tuple); } } }
广告