Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#

Mixed Arrays ใน C# (อาร์เรย์ผสม)

Mixed Arrays = การผสม Multi-dimensional Arrays และ Jagged Arrays

หมายเหตุ: Mixed Arrays ล้าสมัย (Obsolete) ตั้งแต่ .NET 4.0 ลบออกแล้ว

การประกาศ (ไม่แนะนำ)

var x = new object[] {89, 45, "jacob", 9.8};

หรือผสม List:

var x = new object[] {87, 33, "tim", 6.7, new List<string>() {"football","tennis","squash","cricket"} };

ทางเลือกแทน: ใช้ Tuple หรือ List

Tuple<int, string> tuple = new Tuple<int, string>(60, "John");

ตัวอย่าง Tuple

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        Tuple<int, string> tuple = new Tuple<int, string>(99, "Jack");
        if (tuple.Item1 == 99) Console.WriteLine(tuple.Item1);
    }
}