ienumerator

Is there a “HasNext” method for an IEnumerator?

最后都变了- 提交于 2019-11-30 08:59:36
With Java Iterator s, I have used the hasNext method to determine whether an iteration has more elements (without consuming an element) -- thus, hasNext is like a " Peek " method. My question: is there anything like a " hasNext " or " Peek " method with C#'s generic IEnumerator s? No, unfortunately there isn't. The IEnumerator<T> interface only exposes the following members: Methods: Dispose MoveNext Reset Properties : Current No, but in C# you can repeatedly ask for the current element without moving to the next one. It's just a different way of looking at it. It wouldn't be too hard to write

Question regarding IEnumerable and IEnumerator

回眸只為那壹抹淺笑 提交于 2019-11-30 07:25:38
I use the following code to enable myClass to use foreach. But I am rather new to programming and have some difficulty in understanding the following code. I described my problems in the comments. I would be grateful for providing some information. public class MyClass : IEnumerable<string> { //1) What is IEnumerator for? // Whats the difference between IEnumerator and IEnumerable public IEnumerator<string> GetEnumerator() { yield return "first"; yield return "second"; } //2) What is it for? It just calls above method IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } } //3)

Return an empty IEnumerator

百般思念 提交于 2019-11-30 06:01:43
I have an interface that, among other things, implements a "public IEnumerator GetEnumerator()" method, so I can use the interface in a foreach statement. I implement this interface in several classes and in one of them, I want to return an empty IEnumerator. Right now I do this the following way: public IEnumerator GetEnumerator() { ArrayList arr = new ArrayList(); return arr.GetEnumerator(); } However I consider this an ugly hack, and I can't help but think that there is a better way of returning an empty IEnumerator. Is there? This is simple in C# 2: public IEnumerator GetEnumerator() {

Why implement IEnumerable(T) if I can just define ONE GetEnumerator?

故事扮演 提交于 2019-11-29 13:13:42
Update : I appreciate all of the comments, which have essentially comprised unanimous opposition. While every objection raised was valid, I feel that the ultimate nail in the coffin was Ani's astute observation that, ultimately, even the one miniscule benefit that this idea ostensibly offered -- the elimination of boilerplate code -- was negated by the fact that the idea itself would require its own boilerplate code. So yeah, consider me convinced: it would be a bad idea. And just to sort of salvage my dignity somewhat: I might have played it up for argument's sake, but I was never really sold

Return an empty IEnumerator

不羁岁月 提交于 2019-11-29 05:45:43
问题 I have an interface that, among other things, implements a "public IEnumerator GetEnumerator()" method, so I can use the interface in a foreach statement. I implement this interface in several classes and in one of them, I want to return an empty IEnumerator. Right now I do this the following way: public IEnumerator GetEnumerator() { ArrayList arr = new ArrayList(); return arr.GetEnumerator(); } However I consider this an ugly hack, and I can't help but think that there is a better way of

Unity - IEnumerator's yield return null

坚强是说给别人听的谎言 提交于 2019-11-28 10:16:39
I'm currently trying to understand IEnumerator & Coroutine within the context of Unity and am not too confident on what the "yield return null" performs. At the moment i believe it basically pauses and waits for the next frame and in the next frame it'll go back to perform the while statement again. If i leave out the "yield return null" it seems the object will instantly move to its destination or perhaps "skip a lot of frames". So i guess my question is how does this "yield return null" function within this while loop and why is it necessary to have it. void Start () { StartCoroutine(Move())

Why implement IEnumerable(T) if I can just define ONE GetEnumerator?

余生长醉 提交于 2019-11-28 07:07:53
问题 Update : I appreciate all of the comments, which have essentially comprised unanimous opposition. While every objection raised was valid, I feel that the ultimate nail in the coffin was Ani's astute observation that, ultimately, even the one miniscule benefit that this idea ostensibly offered -- the elimination of boilerplate code -- was negated by the fact that the idea itself would require its own boilerplate code. So yeah, consider me convinced: it would be a bad idea. And just to sort of

What is the best way to convert an IEnumerator to a generic IEnumerator?

你说的曾经没有我的故事 提交于 2019-11-27 23:26:09
I am writing a custom ConfigurationElementCollection for a custom ConfigurationHandler in C#.NET 3.5 and I am wanting to expose the IEnumerator as a generic IEnumerator. What would be the best way to achieve this? I am currently using the code: public new IEnumerator<GenericObject> GetEnumerator() { var list = new List(); var baseEnum = base.GetEnumerator(); while(baseEnum.MoveNext()) { var obj = baseEnum.Current as GenericObject; if (obj != null) list.Add(obj); } return list.GetEnumerator(); } Cheers I don't believe there's anything in the framework, but you could easily write one:

How Can I Use IEnumerator.Reset()?

此生再无相见时 提交于 2019-11-27 20:24:31
How exactly is the right way to call IEnumerator.Reset ? The documentation says: The Reset method is provided for COM interoperability. It does not necessarily need to be implemented; instead, the implementer can simply throw a NotSupportedException . Okay, so does that mean I'm not supposed to ever call it? It's so tempting to use exceptions for flow control: using (enumerator = GetSomeExpensiveEnumerator()) { while (enumerator.MoveNext()) { ... } try { enumerator.Reset(); } //Try an inexpensive method catch (NotSupportedException) { enumerator = GetSomeExpensiveEnumerator(); } //Fine, get

obtain generic enumerator from an array

元气小坏坏 提交于 2019-11-27 19:34:00
In C#, how does one obtain a generic enumerator from a given array? In the code below, MyArray is an array of MyType objects. I'd like to obtain MyIEnumerator in the fashion shown, but it seems that I obtain an empty enumerator (although I've confirmed that MyArray.Length > 0 ). MyType [ ] MyArray = ... ; IEnumerator<MyType> MyIEnumerator = ( MyArray.GetEnumerator() as IEnumerator<MyType> ) ; Works on 2.0+: ((IEnumerable<MyType>)myArray).GetEnumerator() Works on 3.5+ (fancy LINQy, a bit less efficient): myArray.Cast<MyType>().GetEnumerator() // returns IEnumerator<MyType> You can decide for