C# 动态数组:使用 List<T> 添加元素
C# 中使用 List<T> 类来实现动态数组,可以使用 Add 方法向 List 中添加元素。
示例代码:
using System;
using System.Collections.Generic;
class Program
{
static void Main(string[] args)
{
List<int> numbers = new List<int>();
// 添加元素
numbers.Add(1);
numbers.Add(2);
numbers.Add(3);
// 输出数组元素
foreach (int number in numbers)
{
Console.WriteLine(number);
}
}
}
输出结果:
1
2
3
原文地址: https://www.cveoy.top/t/topic/oUHu 著作权归作者所有。请勿转载和采集!