如何在 C# 中动态创建数组?
csharpprogrammingserver side programming更新于 2025/5/19 19:22:17
动态数组是可增长的数组,比静态数组有优势。这是因为数组的大小是固定的。
要在 C# 中动态创建数组,请使用 ArrayList 集合。它表示可以单独索引的对象的有序集合。它还允许动态内存分配,添加、搜索和排序列表中的项目。
以下是展示如何在 C# 中动态创建数组的示例。
示例
using System;
using System.Collections;
namespace CollectionApplication {
class Program {
static void Main(string[] args) {
ArrayList al = new ArrayList();
al.Add(99);
al.Add(47);
al.Add(64);
Console.WriteLine("Count: {0}", al.Count);
Console.Write("List: ");
foreach (int i in al) {
Console.Write(i + " ");
}
Console.WriteLine();
Console.ReadKey();
}
}
}
输出
Count: 3 List: 99 47 64

