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

Tạo tuple 6 phần tử trong C#

C# hỗ trợ tuple đến 8 phần tử. Ví dụ tạo tuple 6 item: string, string[], int, int, int, int.

Ví dụ

using System;

class Demo
{
    static void Main()
    {
        var myTuple = new Tuple(
            "electronics",
            new string[] { "shoes", "clothing#", "accessories" },
            100, 250, 500, 1000);

        Console.WriteLine(myTuple.Item1); // electronics
        Console.WriteLine(myTuple.Item5); // 500
        Console.WriteLine(myTuple.Item6); // 1000
    }
}

Kết quả

electronics
500
1000

Lưu ý: Từ C# 7.0, có thể dùng ValueTuple (struct) với cú pháp (string, string[], int, int, int, int) nhẹ hơn.