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

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

Trong C#, Tuple cho phép lưu trữ nhiều giá trị. Để truy cập phần tử thứ ba, bạn sử dụng thuộc tính Item3. Tuple hỗ trợ tối đa 7 phần tử truy cập trực tiếp (Item1-Item7), phần tử thứ 8 trở đi truy cập qua Rest.

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 3rd = " + tuple1.Item3);  // Phần tử thứ 3
        Console.WriteLine("Tuple2 Item 3rd = " + tuple2.Item3);  // Phần tử thứ 3
        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);
        Console.WriteLine("Tuple2 Item 6th = " + tuple2.Item6);
        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 3rd = 500
Tuple2 Item 3rd = 500
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)
    {
        // Tuple 8 phần tử: 7 phần tử đầu + Rest (Tuple lồng)
        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);  // Phần tử thứ 3 = 2200
        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);
        Console.WriteLine("Tuple Item 8th = " + tuple.Rest.Item1); // Phần tử thứ 8
    }
}

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

Cấu trúc Tuple trong .NET

Số phần tử Loại Tuple Truy cập phần tử cuối
1 Tuple<T1> Item1
2 Tuple<T1, T2> Item2
3 Tuple<T1, T2, T3> Item3
4 Tuple<T1, T2, T3, T4> Item4
5 Tuple<T1, T2, T3, T4, T5> Item5
6 Tuple<T1, T2, T3, T4, T5, T6> Item6
7 Tuple<T1, T2, T3, T4, T5, T6, T7> Item7
8+ Tuple<T1...T7, TRest> Rest.Item1, Rest.Rest.Item1...

ValueTuple - Cách hiện đại (C# 7.0+)

Khuyến nghị sử dụng ValueTuple thay vì Tuple class:

// Cú pháp ngắn gọn
var person = (Name: "John", Age: 30, City: "New York", Country: "USA", Zip: 10001);

// Truy cập bằng tên (readable)
Console.WriteLine(person.Name);    // John
Console.WriteLine(person.Age);     // 30
Console.WriteLine(person.City);    // New York - phần tử thứ 3

// Hoặc dùng Item1, Item2, Item3...
Console.WriteLine(person.Item3);   // New York

// Deconstruction
var (name, age, city, country, zip) = person;

So sánh Tuple vs ValueTuple

Đặc điểm Tuple (Class) ValueTuple (Struct)
Kiểu Reference type Value type
Hiệu năng Heap allocation Stack allocation (nhanh hơn)
Đặt tên phần tử Không Có (Name, Age...)
Mutability Immutable Mutable
Số phần tử max 8 (lồng nhau) Không giới hạn

Lưu ý

  • Tuple class là immutable - không thể thay đổi giá trị sau khi tạo
  • ValueTuple cho phép gán giá trị mới: person.Age = 31
  • Cần System.ValueTuple NuGet cho .NET Framework cũ
  • Từ .NET Core 2.0 / .NET Standard 2.0 / .NET 5+ đã built-in