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

Cách lấy phần tử thứ sáu của Tuple trong C#

Để truy cập phần tử thứ sáu của Tuple trong C#, bạn sử dụng thuộc tính Item6. Tuple class hỗ trợ tối đa 7 thuộc tính trực tiếp (Item1 đến Item7).

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());
        
        Console.WriteLine("Tuple1 Item 1st = " + tuple1.Item1);
        Console.WriteLine("Tuple2 Item 1st = " + tuple2.Item1);
        Console.WriteLine("Tuple1 Item 2nd = " + tuple1.Item2);
        Console.WriteLine("Tuple2 Item 2nd = " + tuple2.Item2);
        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);
        Console.WriteLine("Tuple1 Item 6th = " + tuple1.Item6);  // Phần tử thứ 6
        Console.WriteLine("Tuple2 Item 6th = " + tuple2.Item6);  // Phần tử thứ 6
        Console.WriteLine("Tuple1 Item 7th = " + tuple1.Item7);
        Console.WriteLine("Tuple2 Item 7th = " + tuple2.Item7);
    }
}

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
Tuple1 Item 6th = 1200
Tuple2 Item 6th = 1200
Tuple1 Item 7th = 1500
Tuple2 Item 7th = 1500

Ví dụ 2: Tuple 8 phần tử

using System;

public class Demo
{
    public static void Main(string[] args)
    {
        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);
        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);  // Phần tử thứ 6 = 3500
        Console.WriteLine("Tuple Item 7th = " + tuple.Item7);
        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

Tóm tắt truy cập các phần tử

// Tuple 7 phần tử
var t7 = Tuple.Create(1, 2, 3, 4, 5, 6, 7);
t7.Item1  // 1
t7.Item2  // 2
t7.Item3  // 3
t7.Item4  // 4
t7.Item5  // 5
t7.Item6  // 6  ← Phần tử thứ 6
t7.Item7  // 7

// Tuple 8 phần tử (phần tử 8 trong Rest)
var t8 = Tuple.Create(1, 2, 3, 4, 5, 6, 7, 8);
t8.Item6        // 6
t8.Rest.Item1   // 8 (phần tử thứ 8)

// Tuple 9+ phần tử (lồng nhiều cấp)
var t9 = Tuple.Create(1, 2, 3, 4, 5, 6, 7, Tuple.Create(8, 9));
t8.Rest.Item1        // 8
t8.Rest.Rest.Item1   // 9

ValueTuple - Cách tốt hơn (C# 7.0+)

// ValueTuple - không giới hạn số phần tử, có tên
var data = (A: 1, B: 2, C: 3, D: 4, E: 5, F: 6, G: 7, H: 8, I: 9);

Console.WriteLine(data.F);     // 6 - phần tử thứ 6, truy cập bằng tên
Console.WriteLine(data.Item6); // 6 - cũng truy cập bằng Item6

// Deconstruction
var (a, b, c, d, e, f, g, h, i) = data;
Console.WriteLine(f); // 6

Ưu điểm ValueTuple

  • Hiệu năng tốt hơn - value type (struct), stack allocation
  • Đặt tên phần tử - code dễ đọc, ít bug
  • Không giới hạn số lượng - không cần lồng Tuple
  • Mutable - có thể gán giá trị mới
  • Deconstruction - tách biến dễ dàng

Lưu ý

  • Tuple class (System.Tuple) đã cũ, chỉ dùng cho legacy code
  • ValueTuple cần package System.ValueTuple cho .NET Framework < 4.7
  • Tuple class immutable, ValueTuple mutable
  • So sánh Tuple dùng Equals() (so sánh giá trị)