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
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP