C#高级编程三十五天
在foreach语句中使用枚举,可以迭代集合中的元素,且无需知道集合中的元素个数.
数组或集合实现带GetEumerator()方法的IEumerable接口.GetEumerator()方法返回一个实现IEunmerable接口的枚举.
GetEnumerator()方法用IEnumerable接口定义.foreach语句并不真的需要在集合类中实现这个借口.有一个名为GetEnumerator()的方法,他返回实现了IEnumerator接口的对象就足够了.
IEnumerator接口
foreach语句使用IEnumerator接口的方法和属性,迭代集合中的所有元素.为此IEnumerator定义了Current属性,来返回光标所在的元素,该接口的MoveNext()方法移动到集合的下一个元素上,如果有这个元素,该方法就返回true.如果集合不再有更多的元素,该方法就返回false.
这个借口的泛型版本IEnumerator<T>派生自接口IDisposable,因此定义了Dispose()方法,来清理枚举器占用的资源.
foreach语句
C#的foreach语句不会解释为IL代码中的foreach语句.C#编译器会把foreach语句转换为IEnumerable接口的方法和属性.案例:
int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8 };
foreach (var item in arr)
{
Console.WriteLine(item);
}
很明显,foreach语句很简洁,但是他的有点不仅仅在于此,它的效率也是很高的,不用考虑数组是几维的.案例:
int[,] array = new int[8, 8];
for (int i = 0; i < array.GetLength(0); i++)
{
for (int j = 0; j < array.GetLength(1); j++)
{
Console.WriteLine(array[i,j].ToString());
}
}
Console.ReadKey();
使用foreach:
foreach (int item in array)
{
Console.WriteLine(item.ToString());
}
对于三维或者更多维,foreach语句不用发生任何变化,而对于for语句就要进行修改了.
foreach完成类型转换操作,案例:
int[] array = new int[100];
ArrayList aList = new ArrayList();
aList.AddRange(array);
foreach (int item in aList)
{
Console.WriteLine(item.ToString());
}
for (int i = 0; i < aList.Count; i++)
{
int n = (int)aList[i];
Console.WriteLine(n.ToString());
}
Console.ReadKey();
foreach并没有增加资源使用,由于对于继承了IEnumerable接口的数据类型,才能使用foreach语句,那么对于使用foreach会访问IEnumerable接口中的GetEnumerator()方法来进行枚举,那么对应如上的foreach语句,对应的语句如下:
IEnumerator it = aList.GetEnumerator() as IEnumerator;
using (IDisposable disp = it as IDisposable)
{
while (it.MoveNext())
{
int elem = (int)it.Current;
Console.WriteLine(elem.ToString());
}
}
也即是说再出了foreach语句之后对于IEnumerator的对象也进行IDispose处理.
foreach的两种限制
不能修改枚举成员:
int[] array = new int[100];
foreach (var item in array)
{
item++;//这是错误的,因为枚举成员是只读的
Console.WriteLine(item.ToString());
}
不要对集合进项删除操作:
int[] array = new int[100];
ArrayList alist = new ArrayList();
alist.AddRange(array);
foreach (var item in alist)
{
alist.Remove(item);//这是错误的
Console.WriteLine(item.ToString());
}
温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/70146.html