如何在 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