คลาส Tuple
ตัวอย่าง
ให้เรามาดูตัวอย่างการใช้ 2-tuple ใน C# -
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 = jack Exists: Tuple Value = jack
ตัวอย่าง
ให้เราดูตัวอย่างอื่นเพื่อนำ 2-tuple ไปใช้ใน C# -
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 = 20 Exists: Tuple Value = 20