Lớp Tuple<T1,T2,T3,T4,T5,T6> đại diện cho một 6-tuple. Tuple là một cấu trúc dữ liệu có chuỗi các phần tử.
Các trường hợp sử dụng
- Truy cập dễ dàng hơn vào tập dữ liệu.
- Thao tác dễ dàng hơn với tập dữ liệu.
- Đại diện cho một tập dữ liệu đơn lẻ.
- Trả về nhiều giá trị từ một phương thức.
- Truyền nhiều giá trị cho một phương thức.
Lớp này có sáu thuộc tính:
- Item1 — Thành phần thứ nhất.
- Item2 — Thành phần thứ hai.
- Item3 — Thành phần thứ ba.
- Item4 — Thành phần thứ tư.
- Item5 — Thành phần thứ năm.
- Item6 — Thành phần thứ sáu.
Ví dụ 1
using System;
public class Demo {
public static void Main(string[] args) {
Tuple<string, int, string, int, int, string> tuple = new Tuple<string, int, string, int, int, string>("jack", 150, "pete", 300, 600, "allan");
Console.WriteLine("Giá trị (Item1) = " + tuple.Item1);
Console.WriteLine("Giá trị (Item2) = " + tuple.Item2);
Console.WriteLine("Giá trị (Item3) = " + tuple.Item3);
Console.WriteLine("Giá trị (Item4) = " + tuple.Item4);
Console.WriteLine("Giá trị (Item5) = " + tuple.Item5);
Console.WriteLine("Giá trị (Item6) = " + tuple.Item6);
if (tuple.Item1 == "kevin") {
Console.WriteLine("Tồn tại: Tuple Item 1 = " + tuple.Item1);
}
if (tuple.Item2 == 250) {
Console.WriteLine("Tồn tại: Tuple Item 2 = " + tuple.Item2);
}
if (tuple.Item3 == "pete") {
Console.WriteLine("Tồn tại: Tuple Item 3 = " + tuple.Item3);
}
if (tuple.Item4 == 300) {
Console.WriteLine("Tồn tại: Tuple Item 4 = " + tuple.Item4);
}
if (tuple.Item5 == 400) {
Console.WriteLine("Tồn tại: Tuple Item 5 = " + tuple.Item5);
}
if (tuple.Item6 == "allan") {
Console.WriteLine("Tồn tại: Tuple Item 6 = " + tuple.Item6);
}
}
}
Kết quả
Giá trị (Item1) = jack Giá trị (Item2) = 150 Giá trị (Item3) = pete Giá trị (Item4) = 300 Giá trị (Item5) = 600 Giá trị (Item6) = allan Tồn tại: Tuple Item 3 = pete Tồn tại: Tuple Item 4 = 300 Tồn tại: Tuple Item 6 = allan
Ví dụ 2
using System;
public class Demo {
public static void Main(string[] args) {
Tuple<int, int, int, int, int, int> tuple = new Tuple<int, int, int, int, int, int>(100, 150, 200, 300, 600, 1000);
Console.WriteLine("Giá trị (Item1) = " + tuple.Item1);
Console.WriteLine("Giá trị (Item2) = " + tuple.Item2);
Console.WriteLine("Giá trị (Item3) = " + tuple.Item3);
Console.WriteLine("Giá trị (Item4) = " + tuple.Item4);
Console.WriteLine("Giá trị (Item5) = " + tuple.Item5);
Console.WriteLine("Giá trị (Item6) = " + tuple.Item6);
if (tuple.Item1 == 100) {
Console.WriteLine("Tồn tại: Tuple Item 1 = " + tuple.Item1);
}
if (tuple.Item2 == 250) {
Console.WriteLine("Tồn tại: Tuple Item 2 = " + tuple.Item2);
}
if (tuple.Item3 == 300) {
Console.WriteLine("Tồn tại: Tuple Item 3 = " + tuple.Item3);
}
if (tuple.Item4 == 300) {
Console.WriteLine("Tồn tại: Tuple Item 4 = " + tuple.Item4);
}
if (tuple.Item5 == 400) {
Console.WriteLine("Tồn tại: Tuple Item 5 = " + tuple.Item5);
}
if (tuple.Item6 == 500) {
Console.WriteLine("Tồn tại: Tuple Item 6 = " + tuple.Item6);
}
}
}
Kết quả
Giá trị (Item1) = 100 Giá trị (Item2) = 150 Giá trị (Item3) = 200 Giá trị (Item4) = 300 Giá trị (Item5) = 600 Giá trị (Item6) = 1000 Tồn tại: Tuple Item 1 = 100 Tồn tại: Tuple Item 4 = 300