Lớp Tuple<T1, T2> biểu diễn 2-tuple, còn gọi là pair (cặp). Tuple là cấu trúc dữ liệu có trình tự các phần tử.
Ví dụ 1
using System;
public class Demo
{
public static void Main(string[] args)
{
Tuple tuple = new Tuple("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);
}
}
}
Đầu ra
Value = jack Exists: Tuple Value = jack
Ví dụ 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);
}
}
}
Đầu ra
Value = 20 Exists: Tuple Value = 20