如何在 C# 中宣告和初始化一個列表?
要在 C# 中宣告並初始化一個列表,首先宣告列表 −
List<string> myList = new List<string>()
現在新增元素 −
List<string> myList = new List<string>() {
"one",
"two",
"three",
};透過此步驟,我們在上面添加了六個元素。
以下是使用 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
廣告
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
安卓
Python
C 語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP