我们可以在ienumerable中添加或删除但在ienumerable内部返回ienumerator只有为什么ienumerable允许集合修改
问题描述:
嗨Frends,
有非常基本的疑问,要求你帮助理解。
我们可以在不可数的情况下添加或删除,但是在ienumerable内部返回ienumerater只有为什么ienumerable允许集合修改。
和为什么ienumerator不允许修改。
或为什么ienumerater是readonly。
谢谢
Raja
Hi Frends ,
had very basic doubt , requesting you help in understand.
we can add or delete in ienumerable ,but in ienumerable internally returns ienumerater only why ienumerable allows collection modify.
and why ienumerator not allows modify.
or why ienumerater is readonly.
Thanks
Raja
答
首先,IEnumerable和IEnumerator都是接口。您可以将IEnumerable视为一个容器,其中包含一个返回IEnumeator类型对象的方法。
其次,当您创建自己的自定义集合类时,想要迭代然后你必须从IEnumerable派生来支持迭代那个集合类。
你还可以在你的集合类中编写GetEnumerator方法,它返回IEnumerator对象它可以帮助您或提供基本信息。
如1)迭代集合的当前对象是什么,2)MoveNext()方法 - 是否有更多的元素迭代的集合??,3)和Reset() - 重置迭代器指针。
希望这有帮助!
Hi,
First of all IEnumerable and IEnumerator are both interfaces. You can think of IEnumerable as a container which contains one method which returns you IEnumeator type of object.
Second, when you are creating your own custom collection class on which you want to iterate then you must derive from IEnumerable to support Iteration over that collection class.
And further you can write the GetEnumerator method in your collection class which returns you IEnumerator object which helps you or provide basic info.
like 1) what is current object which iterating over the collection, 2) MoveNext() method -- are there more elements in collection for iteration??, 3) And Reset() -- resets the iterator pointer.
Hope this helps!