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

วิธีการประกาศและกำหนดค่าเริ่มต้น List ใน C#

ในการประกาศและ initialize List ใน C#:

  1. ประกาศ List:
    List<string> myList = new List<string>();
  2. เพิ่มสมาชิกพร้อม initialize:
    List<string> myList = new List<string>() {
        "one", "two", "three", "four", "five", "six"
    };

ตัวอย่างเต็ม

using System;
using System.Collections.Generic;

class Program {
    static void Main() {
        List<string> myList = new List<string>() {
            "one", "two", "three", "four", "five", "six"
        };
        Console.WriteLine(myList.Count);
    }
}

ผลลัพธ์

6