关于IEnumerable,大家帮我看看

ninglh 2007-10-16 11:54:16
下面是下载的关于IEnumerable的实例,我看不懂,哪位大哥帮忙解释下,讲讲如何实现IEnumerable接口,谢了
class Employees : IEnumerable
{
private ArrayList m_Employees;
private int m_MaxEmployees;

public Employees( int MaxEmployees )
{
m_MaxEmployees = MaxEmployees;
m_Employees = new ArrayList( MaxEmployees );
}

// Here is the implementation of the indexer by array index
public Employee this[int index]
{
get
{
// Check for out of bounds condition
if ( index < 0 || index > m_Employees.Count - 1 )
return null;

// Return employee based on index passed in
return (Employee) m_Employees[index];
}

set
{
// Check for out of bounds condition
if ( index < 0 || index > m_MaxEmployees-1 )
return;

// Add new employee
m_Employees.Insert( index, value );
}
}

// Here is the implementation of the indexer by SSN
public Employee this[string SSN]
{
get
{
Employee empReturned = null;

foreach ( Employee employee in m_Employees )
{
// Return employee based on index passed in
if ( employee.SSN == SSN )
{
empReturned = employee;
break;
}
}

return empReturned;
}
}

// Return the total number of employees.
public int Length
{
get
{
return m_Employees.Count;
}
}

// IEnumerable implementation, delegates IEnumerator to
// the ArrayList
public IEnumerator GetEnumerator()
{
return m_Employees.GetEnumerator();
}
}

class Employee
{
private string m_firstName;
private string m_middleName;
private string m_lastName;
private string m_SSN;

// Constructor
public Employee( string FirstName, string LastName, string
MiddleName, string SSN )
{
m_firstName = FirstName;
m_middleName = MiddleName;
m_lastName = LastName;
m_SSN = SSN;
}

// FirstName property
public string FirstName
{
get { return m_firstName; }
set { m_firstName = value; }
}

// MiddleName property
public string MiddleName
{
get { return m_middleName; }
set { m_middleName = value; }
}

// LastName property
public string LastName
{
get { return m_lastName; }
set { m_lastName = value; }
}

// SSN property
public string SSN
{
get { return m_SSN; }
set { m_SSN = value; }
}
}
...全文
110 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
ninglh 2007-10-17
  • 打赏
  • 举报
回复
怎么结贴啊
vwxyzh 2007-10-16
  • 打赏
  • 举报
回复
错了,这个例子不是很好
IEnumerable是为foreach量身定做的
核心方法就是GetEnumerator,用来获得一个IEnumarator的实例。
(例子中没有这个部分的实现,所以说不好。)

至于索引器、Length这些都和IEnumerable无关
brucenan999 2007-10-16
  • 打赏
  • 举报
回复
你可以像数组一样操作Employees

Employees[i]
Employees.Length

继承IEnumerable 就是提供这样的功能.

vwxyzh 2007-10-16
  • 打赏
  • 举报
回复
不一样,例如Stack就只能foreach遍历,不能用索引器
virusswb 2007-10-16
  • 打赏
  • 举报
回复
不是像数组一样,那是this索引器的功能

这个接口是你可以在你的类实例上使用foreach来枚举
vwxyzh 2007-10-16
  • 打赏
  • 举报
回复
可以参考一下ArrayList的实现:

public virtual IEnumerator GetEnumerator()
{
return new ArrayListEnumeratorSimple(this);
}


其中ArrayListEnumeratorSimple类的实现(有点复杂,不需要全看懂):

[Serializable]
private sealed class ArrayListEnumeratorSimple : IEnumerator, ICloneable
{
private object currentElement;
private static object dummyObject = new object();
private int index;
[NonSerialized]
private bool isArrayList;
private ArrayList list;
private int version;

internal ArrayListEnumeratorSimple(ArrayList list)
{
this.list = list;
this.index = -1;
this.version = list._version;
this.isArrayList = list.GetType() == typeof(ArrayList);
this.currentElement = dummyObject;
}

public object Clone()
{
return base.MemberwiseClone();
}

public bool MoveNext()
{
if (this.version != this.list._version)
{
throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_EnumFailedVersion"));
}
if (this.isArrayList)
{
if (this.index < (this.list._size - 1))
{
this.currentElement = this.list._items[++this.index];
return true;
}
this.currentElement = dummyObject;
this.index = this.list._size;
return false;
}
if (this.index < (this.list.Count - 1))
{
this.currentElement = this.list[++this.index];
return true;
}
this.index = this.list.Count;
this.currentElement = dummyObject;
return false;
}

public void Reset()
{
if (this.version != this.list._version)
{
throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_EnumFailedVersion"));
}
this.currentElement = dummyObject;
this.index = -1;
}

public object Current
{
get
{
object currentElement = this.currentElement;
if (dummyObject != currentElement)
{
return currentElement;
}
if (this.index == -1)
{
throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_EnumNotStarted"));
}
throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_EnumEnded"));
}
}
}

其中核心成员是MoveNext()和Current
MoveNext有点复杂(因为额外做了版本检查等事情),简单的说就是记录一个index,每次MoveNext()就把index++,取出ArrayList第index处的元素,放在currentElement里面,返回true,如果无法MoveNext(就是后面没有元素了),就返回false
再取Current属性时返回currentElement
ninglh 2007-10-16
  • 打赏
  • 举报
回复
谢谢上面两位大哥的发言,能不能再说清楚点,主要关于GetEnumerator方法的,前面的我已经知道了,这个例子前面是把讲索引部分加在里面了,IEnumerable的运用只是用到了里面的
// IEnumerable implementation, delegates IEnumerator to
// the ArrayList
public IEnumerator GetEnumerator()
{
return m_Employees.GetEnumerator();
}

110,535

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧