在C#中,遍历ArrayList的方法有很多种。以下是一些常见的方法:
ArrayList myArrayList = new ArrayList();
// 添加元素到ArrayList
myArrayList.Add("Apple");
myArrayList.Add("Banana");
myArrayList.Add("Cherry");
for (int i = 0; i < myArrayList.Count; i++)
{
Console.WriteLine(myArrayList[i]);
}
ArrayList myArrayList = new ArrayList();
// 添加元素到ArrayList
myArrayList.Add("Apple");
myArrayList.Add("Banana");
myArrayList.Add("Cherry");
foreach (object item in myArrayList)
{
Console.WriteLine(item);
}
ArrayList myArrayList = new ArrayList();
// 添加元素到ArrayList
myArrayList.Add("Apple");
myArrayList.Add("Banana");
myArrayList.Add("Cherry");
IEnumerator iterator = myArrayList.GetEnumerator();
while (iterator.MoveNext())
{
Console.WriteLine(iterator.Current);
}
以上三种方法都可以实现遍历ArrayList的目的,你可以根据自己的需求和喜好选择合适的方法。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读:c#中arraylist怎么删除重复值