C# 枚举器

时间:2023-12-29 09:26:02

1:枚举器和可枚举类型

我们知道使用foreach可以遍历数组中的元素。那么为什么数组可以被foreach语句处理呢,下面我们就进行讨论一下这个问题。

2:使用foreach语句

我们知道当我们使用foreach语句的时候,这个语句为我们依次取出了数组中的每一个元素。

例如下面的代码:

             int[] arr = { , , , , ,  };
foreach( int arry in arr )
{
Console.WriteLine("Array Value::{0}",arry);
}

输出效果为

C# 枚举器

为什么数组可以使用foreach来遍历,原因是数组可以按需提供一个叫做枚举器(enumerator)的对象,枚举器可以依次返回请求的数组中的元素,枚举器知道项的次序并且跟踪它在序列中的位置。依次返回请求的当前项。

  对于有枚举器的类型而言,必须有一个方法来获取它这个类型。获取一个对象枚举器的方法是调用对象的GetEnumrator方法,实现GetEnumrator方法的类型叫做可枚举类型。那么数组就是可枚举类型。

  下图演示一下可枚举类型和枚举器之间的关系。

C# 枚举器

foreach结构设计用来和可枚举类型一起使用,只要给它的遍历对象是可枚举类型,比如数组。

1:通过调用GetEnumrator方法获取对象的枚举器。

2:从枚举器中请求每一项并且把它作为迭代器,代码可以读取该变量,但不可以改变

foreach(Type VarName in EnumrableObject )

{

  ...

}

EnumrableObjec必须是可枚举类型。

2:IEnumrator接口

IEnumrator接口包含了3个函数成员:Current、MoveNext以及Reset;

.Current是返回序列中当前位置项的属性。(注意:Current它是只读属性,它返回Object类型的引用,所以可以返回任意类型)

.MoveNext是把枚举器位置前进到集合中下一项的方法。它也但会布尔值,指示新的位置是否是有效位置。

注:如果返回的位置是有效的,方法返回true;

  如果新的位置是无效的,方法返回false;

  枚举器的原始位置在序列中的第一项之前,因此MoveNext必须在第一次使用Current之前调用。

.Reset是把位置重置为原始状态的方法。

下面我们用图表示一下他们之间的关系

C# 枚举器

有了集合的枚举器,我们就可以使用MoveNext和Current成员来模仿foreach循环遍历集合中的项,例如,我们已经知道数组是可枚举类型,所以下面的代码手动做foreach语句

自动做的事情。

代码如下:

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections; namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int[] arr = { , , , , , };
IEnumerator ie = arr.GetEnumerator();
while( ie.MoveNext() )
{
int i = (int)ie.Current;
Console.WriteLine("{0}", i);
}
}
}
}

程序运行的结果为

C# 枚举器

我们来用图解释一下代码中的数组结构

C# 枚举器

IEnumerable接口

数组是可枚举类型,是因为实现了IEnumerable接口的类,所以可枚举类都是因为实现了IEnumerable接口的类。

IEnumerable接口只有一个成员——GetEnumerator()方法,它返回对象的枚举器。

如图所示:

C# 枚举器

下面我们举一个使用IEnumerator和IEnumerable的例子

下面的代码展示了一个可枚举类的完整示例,该类叫Component(球形)。它的枚举器类为Shape(形状)。

代码如下:

 using System;
using System.Collections; namespace ConsoleApplication1
{
class Shape : IEnumerator
{
string[] _Shapes;
int _Position = -; public Shape(string[] _theShapes)
{
_Shapes = new string[_theShapes.Length];
for( int i = ; i < _theShapes.Length; i++ )
{
_Shapes[i] = _theShapes[i];
}
} public Object Current
{
get
{
if ( _Position == - )
throw new InvalidOperationException();
if (_Position >= _Shapes.Length)
throw new InvalidOperationException();
return _Shapes[_Position];
}
} public bool MoveNext()
{
if (_Position < _Shapes.Length - )
{
_Position++;
return true;
}
else
return false;
} public void Reset()
{
_Position = -;
}
} class Component : IEnumerable
{
string[] shapes = { "Circular", "spherical", "Quadrilateral", "Label" };
public IEnumerator GetEnumerator()
{
return new Shape( shapes );
}
} class Program
{
static void Main(string[] args)
{
Component comp = new Component();
foreach ( string oshape in comp )
Console.WriteLine(oshape);
} }
}

运行结果:

C# 枚举器

如果您看了本篇博客,觉得对您有所收获,请点击右下角的 [推荐]

如果您想转载本博客,请注明出处

如果您对本文有意见或者建议,欢迎留言

感谢您的阅读,请关注我的后续博客