Trong C#, Tuple là một cấu trúc dữ liệu cho phép lưu trữ nhiều giá trị có kiểu dữ liệu khác nhau. Để truy cập phần tử thứ hai, bạn sử dụng thuộc tính Item2.
Ví dụ 1: Tuple 7 phần tử
using System;
public class Demo
{
public static void Main(string[] args)
{
var tuple1 = Tuple.Create(75, 200, 500, 700, 100, 1200, 1500);
var tuple2 = Tuple.Create(75, 200, 500, 700, 100, 1200, 1500);
Console.WriteLine("Tuple1 bằng Tuple2? = " + tuple1.Equals(tuple2));
Console.WriteLine("HashCode của Tuple1 = " + tuple1.GetHashCode());
Console.WriteLine("HashCode của Tuple2 = " + tuple2.GetHashCode());
// Truy cập các phần tử
Console.WriteLine("Tuple1 Item 1st = " + tuple1.Item1);
Console.WriteLine("Tuple2 Item 1st = " + tuple2.Item1);
Console.WriteLine("Tuple1 Item 2nd = " + tuple1.Item2); // Phần tử thứ 2
Console.WriteLine("Tuple2 Item 2nd = " + tuple2.Item2); // Phần tử thứ 2
Console.WriteLine("Tuple1 Item 4th = " + tuple1.Item4);
Console.WriteLine("Tuple2 Item 4th = " + tuple2.Item4);
Console.WriteLine("Tuple1 Item 5th = " + tuple1.Item5);
Console.WriteLine("Tuple2 Item 5th = " + tuple2.Item5);
}
}
Kết quả
Tuple1 bằng Tuple2? = True HashCode của Tuple1 = 3231587 HashCode của Tuple2 = 3231587 Tuple1 Item 1st = 75 Tuple2 Item 1st = 75 Tuple1 Item 2nd = 200 Tuple2 Item 2nd = 200 Tuple1 Item 4th = 700 Tuple2 Item 4th = 700 Tuple1 Item 5th = 100 Tuple2 Item 5th = 100
Ví dụ 2: Tuple 8 phần tử (sử dụng Tuple<T1...T8>)
using System;
public class Demo
{
public static void Main(string[] args)
{
// Tuple với 8 phần tử cần nested Tuple cho phần tử cuối
var tuple = Tuple.Create(1200, 1500, 2200, 2700, 3100, 3500, 4500, 5500);
Console.WriteLine("HashCode của Tuple = " + tuple.GetHashCode());
Console.WriteLine("Tuple Item 1st = " + tuple.Item1);
Console.WriteLine("Tuple Item 2nd = " + tuple.Item2); // Phần tử thứ 2
Console.WriteLine("Tuple Item 3rd = " + tuple.Item3);
Console.WriteLine("Tuple Item 4th = " + tuple.Item4);
Console.WriteLine("Tuple Item 5th = " + tuple.Item5);
Console.WriteLine("Tuple Item 6th = " + tuple.Item6);
Console.WriteLine("Tuple Item 7th = " + tuple.Item7);
// Phần tử thứ 8 truy cập qua tuple.Rest.Item1
Console.WriteLine("Tuple Item 8th = " + tuple.Rest.Item1);
}
}
Kết quả
HashCode của Tuple = 49989024 Tuple Item 1st = 1200 Tuple Item 2nd = 1500 Tuple Item 3rd = 2200 Tuple Item 4th = 2700 Tuple Item 5th = 3100 Tuple Item 6th = 3500 Tuple Item 7th = 4500 Tuple Item 8th = 5500
Lưu ý quan trọng
- Tuple trong C# là bất biến (immutable) - không thể thay đổi giá trị sau khi tạo
- Truy cập phần tử qua
Item1,Item2...Item7 - Phần tử thứ 8 trở đi truy cập qua thuộc tính
Rest - Từ C# 7.0, nên sử dụng ValueTuple (cú pháp
(int, string, bool)) cho hiệu năng tốt hơn và hỗ trợ đặt tên phần tử
So sánh Tuple vs ValueTuple
| Đặc điểm | Tuple (Reference Type) | ValueTuple (Value Type) |
|---|---|---|
| Hiệu năng | Chậm hơn (heap allocation) | Nhanh hơn (stack allocation) |
| Đặt tên phần tử | Không hỗ trợ | Hỗ trợ (Item1, Name, Age...) |
| Số lượng phần tử tối đa | 8 (phần tử 8 là nested) | Không giới hạn |