集合类型的foreach循环浅析
2008-01-23 20:44
423 查看
.net framework支持任何 Collections 类都可以生成自己的枚举数,该枚举数简化了对元素的循环访问。在c#中,可以使用foreach循环来访问集合中的对象。使用foreach是集合的主要目的,集合没有提供其他特性。
集合必须实现接口System.Collections.IEnumerable. IEnumerable 只定义了一个方法:
public interface IEnumerable
{
IEnumeator GetEnumerator()
}
GetEnumerator 的目的是返回实现了IEnumerator 的枚举对象。在下面的例子中,枚举对象为 ProgramEnumerable。
interface IEnumerator
{
object Current{get;} //read only
bool MoveNext();
void Reset();
}
IEumerator 的工作方式:实现该接口的对象与一个集合相关联 (如 array, arraylist 等),这个对象在第一次初始化时,还没有指向集合的任何元素,必须先调用movenext, 移动枚举,才能使他指向集合中的第一个元素,接着用current属性获取该元素,current 属性返回一个对象引用,所以必须把他的数据类型转换为要在集合中查找的对象类型。 然后再调用movenext方法移动到集合的下一个元素上。直到集合中没有元素为止。即current返回了一个null.
调用reset方法来返回到集合的开头的前面的位置,相当于array 的index = -1 的位置。接着就需要调用movenext把index = 0;
foreach 循环为集合类型提供了一种不需要index的遍历元素的方式,foreach只负责遍历所有的元素,元素的顺序由集合设定。
所以foreach循环实际上是以上IEnumerator, IEnumerable接口的简化版。Array属于集合,但不再System.Collections namespace 下。
集合的current把枚举作为一个object返回,因为多个枚举可应用在同一个集合上。
在 C# 中,集合类并非必须严格从 IEnumerable 和 IEnumerator 继承才能与 foreach 兼容;只要类有所需的 GetEnumerator、MoveNext、Reset 和 Current 成员,便可以与 foreach 一起使用。省略接口的好处为,使您可以将 Current 的返回类型定义得比 object 更明确,从而提供了类型安全。
例如,从上面的示例代码开始,更改以下几行:
现在,由于 Current 返回字符串,因此当 foreach 语句中使用了不兼容的类型时,编译器便能够检测到:
省略 IEnumerable 和 IEnumerator 的缺点是,集合类不再能够与其他公共语言运行库兼容语言的 foreach 语句或等效项交互操作。
您可以同时拥有两者的优点,即 C# 中的类型安全以及与其他公共语言运行库兼容语言的互操作性,方法是从 IEnumerable 和 IEnumerator 继承并使用显式接口实现,如下面的示例所示。
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace IEnumerable1
...{
/**//// <summary>
/// 实现IEnumerable 把它标记为一个集合
/// </summary>
public class Program : IEnumerable
...{
string[] str;
public Program(string text, char[] space)
...{
str = text.Split(space);
}
//实现IEnumerable接口
public IEnumerator GetEnumerator()
...{
//枚举的实例引用
return new ProgramEnumerable(this);
}
/**//// <summary>
/// 私有类, 不能由外部代码访问
/// 定义枚举
/// </summary>
private class ProgramEnumerable: IEnumerator
...{
int position = -1;
Program p;
public ProgramEnumerable(Program pp)
...{
p = pp;
}
public bool MoveNext()
...{
if (position < (p.str.Length - 1))
...{
++position;
return true;
}
else
return false;
}
public object Current
...{
get
...{
if (position > -1 && position < p.str.Length)
return p.str[position];
else
throw new InvalidOperationException("blablabla");
}
}
public void Reset()
...{
position = -1;
}
}
static void Main(string[] args)
...{
Program pp = new Program("I so AM-hungry", new char[] ...{ ' ', '-' });
foreach (string str in pp)
...{
Console.WriteLine("{0}", str);
}
}
}
}
集合必须实现接口System.Collections.IEnumerable. IEnumerable 只定义了一个方法:
public interface IEnumerable
{
IEnumeator GetEnumerator()
}
GetEnumerator 的目的是返回实现了IEnumerator 的枚举对象。在下面的例子中,枚举对象为 ProgramEnumerable。
interface IEnumerator
{
object Current{get;} //read only
bool MoveNext();
void Reset();
}
IEumerator 的工作方式:实现该接口的对象与一个集合相关联 (如 array, arraylist 等),这个对象在第一次初始化时,还没有指向集合的任何元素,必须先调用movenext, 移动枚举,才能使他指向集合中的第一个元素,接着用current属性获取该元素,current 属性返回一个对象引用,所以必须把他的数据类型转换为要在集合中查找的对象类型。 然后再调用movenext方法移动到集合的下一个元素上。直到集合中没有元素为止。即current返回了一个null.
调用reset方法来返回到集合的开头的前面的位置,相当于array 的index = -1 的位置。接着就需要调用movenext把index = 0;
foreach 循环为集合类型提供了一种不需要index的遍历元素的方式,foreach只负责遍历所有的元素,元素的顺序由集合设定。
所以foreach循环实际上是以上IEnumerator, IEnumerable接口的简化版。Array属于集合,但不再System.Collections namespace 下。
集合的current把枚举作为一个object返回,因为多个枚举可应用在同一个集合上。
在 C# 中,集合类并非必须严格从 IEnumerable 和 IEnumerator 继承才能与 foreach 兼容;只要类有所需的 GetEnumerator、MoveNext、Reset 和 Current 成员,便可以与 foreach 一起使用。省略接口的好处为,使您可以将 Current 的返回类型定义得比 object 更明确,从而提供了类型安全。
例如,从上面的示例代码开始,更改以下几行:
// No longer inherits from IEnumerable: public class Tokens // Doesn't return an IEnumerator: public TokenEnumerator GetEnumerator() // No longer inherits from IEnumerator: public class TokenEnumerator // Type-safe: returns string, not object: public string Current
现在,由于 Current 返回字符串,因此当 foreach 语句中使用了不兼容的类型时,编译器便能够检测到:
// Error: cannot convert string to int: foreach (int item in f)
省略 IEnumerable 和 IEnumerator 的缺点是,集合类不再能够与其他公共语言运行库兼容语言的 foreach 语句或等效项交互操作。
您可以同时拥有两者的优点,即 C# 中的类型安全以及与其他公共语言运行库兼容语言的互操作性,方法是从 IEnumerable 和 IEnumerator 继承并使用显式接口实现,如下面的示例所示。
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace IEnumerable1
...{
/**//// <summary>
/// 实现IEnumerable 把它标记为一个集合
/// </summary>
public class Program : IEnumerable
...{
string[] str;
public Program(string text, char[] space)
...{
str = text.Split(space);
}
//实现IEnumerable接口
public IEnumerator GetEnumerator()
...{
//枚举的实例引用
return new ProgramEnumerable(this);
}
/**//// <summary>
/// 私有类, 不能由外部代码访问
/// 定义枚举
/// </summary>
private class ProgramEnumerable: IEnumerator
...{
int position = -1;
Program p;
public ProgramEnumerable(Program pp)
...{
p = pp;
}
public bool MoveNext()
...{
if (position < (p.str.Length - 1))
...{
++position;
return true;
}
else
return false;
}
public object Current
...{
get
...{
if (position > -1 && position < p.str.Length)
return p.str[position];
else
throw new InvalidOperationException("blablabla");
}
}
public void Reset()
...{
position = -1;
}
}
static void Main(string[] args)
...{
Program pp = new Program("I so AM-hungry", new char[] ...{ ' ', '-' });
foreach (string str in pp)
...{
Console.WriteLine("{0}", str);
}
}
}
}
相关文章推荐
- Java中foreach和for在循环遍历数组、集合方面的区别
- 关于在在foreach循环中移除集合中的元素的解决方案
- 自定义类型使用foreach循环
- foreach 循环不能改变集合中各项的值
- 黑马程序员_两种遍历集合的方法Iterator接口和foreach循环
- 记录java,foreach循环中更改了集合会出现java.util.ConcurrentModificationException
- 19-集合框架工具类-20-常用对象API(集合框架-JDK5.0特性-ForEach循环)
- list集合存list 以及s标签在foreach循环遍历中用EL表达式取值
- 跟王老师学集合(四):使用foreach循环遍历元素
- foreach循环遍历数组和集合
- 利用 IEnumerable接口 实现自定义类型的集合的foreach遍历
- Windows Phone开发之集合List和Foreach循环
- 集合框架-Foreach循环
- Jstl的foreach循环中用el表达式输出集合
- 浅析Java中的set集合类型及其接口的用法
- Java学习之容器上(Collection接口常用方法,Iterator接口,使用foreach循环遍历Collection集合元素,Set集合通用知识(Hashset类,hashcode()与LinkedHashSet类))
- foreach 循环不能改变集合中各项的值
- 迭代器、foreach循环、泛型集合
- foreach循环删除集合中的元素出现ConcurrentModificationException异常
- C#使用foreach语句遍历集合类型的方法