什么是C#中的动态数组?
动态数组是可增长的数组,并且比静态数组具有优势。这是因为数组的大小是固定的。
要在C#中动态创建数组,请使用ArrayList集合。它表示可以单独索引的对象的有序集合。它还允许动态分配内存,添加,搜索和排序列表中的项目。
以下是显示如何在C#中动态创建数组的示例-
示例
using System;
using System.Collections;
namespace Demo {
class Program {
static void Main(string[] args) {
ArrayList al = new ArrayList();
al.Add(577);
al.Add(286);
Console.WriteLine("Count: {0}", al.Count);
Console.Write("List: ");
foreach (int i in al) {
Console.Write(i + " ");
}
Console.WriteLine();
Console.ReadKey();
}
}
}输出结果
Count: 2 List: 577 286