阅读量:3
在C#中,遍历ArrayList的方法有很多种。以下是一些常见的方法:
- 使用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]);
}
- 使用foreach循环遍历:
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的目的,你可以根据自己的需求和喜好选择合适的方法。
以上就是关于“c# arraylist怎样进行遍历操作”的相关介绍,筋斗云是国内较早的云主机应用的服务商,拥有10余年行业经验,提供丰富的云服务器、租用服务器等相关产品服务。云服务器资源弹性伸缩,主机vCPU、内存性能强悍、超高I/O速度、故障秒级恢复;电子化备案,提交快速,专业团队7×24小时服务支持!
简单好用、高性价比云服务器租用链接:https://www.jindouyun.cn/product/cvm