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

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

Lớp Tuple<T1,T2,T3,T4,T5,T6,T7> đại diện cho một 7-tuple, còn được gọi là septet. 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ó bảy 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.
  • Item7 — Thành phần thứ bảy.

Ví dụ 1

using System;
public class Demo {
    public static void Main(string[] args) {
        Tuple<int, int, int, int, int, int, int> tuple = new Tuple<int, int, int, int, int, int, int>(100, 150, 200, 300, 600, 1000, 2000);
        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);
        Console.WriteLine("Giá trị (Item7) = " + tuple.Item7);
        if (tuple.Item5 == 600) {
            Console.WriteLine("Tồn tại: Tuple Item 5 = " + tuple.Item5);
        }
        if (tuple.Item6 == 900) {
            Console.WriteLine("Tồn tại: Tuple Item 6 = " + tuple.Item6);
        }
        if (tuple.Item7 == 2000) {
            Console.WriteLine("Tồn tại: Tuple Item 7 = " + tuple.Item7);
        }
    }
}

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
Giá trị (Item7) = 2000
Tồn tại: Tuple Item 5 = 600
Tồn tại: Tuple Item 7 = 2000

Ví dụ 2

using System;
public class Demo {
    public static void Main(string[] args) {
        Tuple<int, int, int, int, int, int, int> tuple = new Tuple<int, int, int, int, int, int, int>(100, 150, 200, 300, 600, 1000, 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);
        Console.WriteLine("Giá trị (Item7) = " + tuple.Item7);
        if (tuple.Item5 == 600) {
            Console.WriteLine("Tồn tại: Tuple Item 5 = " + tuple.Item5);
        }
        if (tuple.Item6 == 900) {
            Console.WriteLine("Tồn tại: Tuple Item 6 = " + tuple.Item6);
        }
        if (tuple.Item7 == 2000) {
            Console.WriteLine("Tồn tại: Tuple Item 7 = " + tuple.Item7);
        }
        if (tuple.Item7 == tuple.Item6) {
            Console.WriteLine("Các Tuple Item khớp nhau!");
        }
    }
}

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
Giá trị (Item7) = 1000
Tồn tại: Tuple Item 5 = 600
Các Tuple Item khớp nhau!