C#에서 목록을 선언하고 초기화하려면 먼저 목록을 선언하십시오 -
List<string> myList = new List<string>()
이제 요소를 추가하십시오 -
List<string> myList = new List<string>() {
"one",
"two",
"three",
}; 이를 통해 위의 6가지 요소를 추가했습니다.
다음은 C#에서 목록을 선언하고 초기화하는 전체 코드입니다. −
예
using System;
using System.Collections.Generic;
class Program {
static void Main() {
// Initializing collections
List<string> myList = new List<string>() {
"one",
"two",
"three",
"four",
"five",
"size"
};
Console.WriteLine(myList.Count);
}
} 출력
6