Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C#

Lớp Tuple<T1,T2,T3,T4,T5> trong C#

Lớp Tuple<T1, T2, T3, T4, T5> đại diện cho một 5-tuple, còn được gọi là quintuple. 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ó năm 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.

Ví dụ 1

using System;
public class Demo {
    public static void Main(string[] args) {
        Tuple<int, int, int, int, int> tuple = new Tuple<int, int, int, int, int>(120, 150, 270, 300, 600);
        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);
        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 == 270) {
            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);
        }
    }
}

Kết quả

Giá trị (Item1) = 120
Giá trị (Item2) = 150
Giá trị (Item3) = 270
Giá trị (Item4) = 300
Giá trị (Item5) = 600
Tồn tại: Tuple Item 3 = 270
Tồn tại: Tuple Item 4 = 300

Ví dụ 2

using System;
public class Demo {
    public static void Main(string[] args) {
        Tuple<string, int, string, int, int> tuple = new Tuple<string, int, string, int, int>("jack", 150, "pete", 300, 600);
        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);
        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);
        }
    }
}

Kết quả

Giá trị (Item1) = jack
Giá trị (Item2) = 150
Giá trị (Item3) = pete
Giá trị (Item4) = 300
Giá trị (Item5) = 600
Tồn tại: Tuple Item 3 = pete
Tồn tại: Tuple Item 4 = 300