便宜VPS主机精选
提供服务器主机评测信息

C#中ArrayList遍历全攻略:高效操作技巧详解

在C#中,遍历ArrayList的方法有很多种。以下是一些常见的方法:

  1. 使用for循环遍历:
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]);
}
  1. 使用foreach循环遍历:
ArrayList myArrayList = new ArrayList();
// 添加元素到ArrayList
myArrayList.Add("Apple");
myArrayList.Add("Banana");
myArrayList.Add("Cherry");

foreach (object item in myArrayList)
{
    Console.WriteLine(item);
}
  1. 使用迭代器遍历:
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的目的,你可以根据自己的需求和喜好选择合适的方法。

未经允许不得转载:便宜VPS测评 » C#中ArrayList遍历全攻略:高效操作技巧详解