ienumerator

Access Enumerator within a foreach loop?

I have a List class, and I would like to override GetEnumerator() to return my own Enumerator class. This Enumerator class would have two additional properties that would be updated as the Enumerator is used. For simplicity (this isn't the exact business case), let's say those properties were CurrentIndex and RunningTotal. I could mana...

Why was GetEnumerator() stored in a separate interface from IEnumerator ?

Hi, I was wondering why the GetEnumerator() method was factored out of IEnumerator and placed in IEnumerable. It seems to me that it would make more sense to keep all of the enumerator methods in IEnumerator. Thanks, Scott ...

IEnumerable and IEnumerator in the same class, bad idea?

Is this a bad idea? Private Class GH_DataStructureEnumerator(Of Q As Types.IGH_Goo) Implements IEnumerable(Of Q) Implements IEnumerator(Of Q) .... .... 'Current, MoveNext, Reset etc.' .... .... Public Function GetEnumerator_Generic() As IEnumerator(Of Q) _ Implements IEnumerable(Of Q).GetEnumerator ...

Why in this example (got from msdn), in GetEnumerator method , new PeopleEnum returns IEnumerator?

Why in this example from MSDN, in GetEnumerator method, PeopleEnum returns IEnumerator? public class Person { public Person(string fName, string lName) { this.firstName = fName; this.lastName = lName; } public string firstName; public string lastName; } public class People : IEnumerable { privat...

Is it safe to delete from SortedList during iteration

Hi, My question is is it safe for enumerator to remove item from SortedList? SortedList<decimal, string> myDictionary; // omitted code IEnumerator<decimal, string> enum = myDictionary.GetEnumerator(); while(enum.MoveNext) { // is it ok to remove here? myDictionary.Remove(enum.Current.Key); } ...

Why do arrays in .net only implement IEnumerable and not IEnumerable<T>?

I was implementing my own ArrayList class and was left surprised when I realised that public System.Collections.Generic.IEnumerator<T> GetEnumerator() { return _array.GetEnumerator(); } didn't work. What is the reason arrays don't implement IEnumerator in .NET? Is there any work-around? Thanks ...

IEnumerable<T> representing the "rest" of an IEnumerable<T> sequence

If I am walking through an IEnumerable<T>, is there any way to get a new IEnumerable<T> representing the remaining items after the current one. For example, I would like to write an extension method IEnumerator<T>.Remaining(): IEnumerable<int> sequence = ... IEnumerator<int> enumerator = sequence.GetEnumerator(); if (enumerator.MoveNe...

[C#] Problems with implementing generic IEnumerator and IComparable

Hi all! I'm working on an AVL Tree. The tree itself seems to be working but I need a iterator to walk through the values of the tree. Therefore I tried to implement the IEnumerator interace. Unfortunately I get a compile time error implementing IEnumerator and IComparable. First the code and below that the error. class AvlTreePreOrderE...

Can we use GetEnumerator() without using IEnumerable interface?

I have a class called Primes and this class implements GetEnumerator() without implementing IEnumerable interface. public class Primes { private long min; private long max; public Primes() : this(2, 100) { } public IEnumerator GetEnumerator() {...} I don't get it. Am I missing something? ...

How to Implement an Interface that Requires Duplicate Member Names?

I often have to implement some interfaces such as IEnumerable<T> in my code. Each time, when implementing automatically, I encounter the following: public IEnumerator<T> GetEnumerator() { // Code here... } public IEnumerator GetEnumerator1() { // Code here... } Though I have to implement both GetEnumerator() methods, they im...

IEnumerator: Is it normal to have an empty Dispose method?

I'm writing an IEnumerator<T> class to iterate over a COM collection I'm wrappering. I've noticed that IEnumerator<T> extends IDisposable, so I'm required to implement the Dispose method. However, I can't think of anything I would put there, as I only have a reference to the collection (which I wouldn't want being disposed at the end...

Pattern for using IEnumerator<T> in interfaces

I have a C# class which needs to process a sequence of items (IEnumerable<T>) across a bunch of methods, so I cannot simply foreach inside a method. I call .GetEnumerator() and pass this IEnumerator<T> around and it works great giving me the flexibility I need while looping through a single sequence. Now I want to allow others to add lo...

How this part of code is working

UPD. Hello, I know how code below is working. I know that cross, and circle are pointing to Cross(), and Circle() method. But I am still filling little confuse with this part of code. Can you explain it for me? public GameMoves() { cross = Cross(); circle = Circle(); } All code...

IEnumerator implementation problem

Hi Guys, I have this code: public class Check21CSVRecord { public string TransactionType; public string TransactionCurrency; public string Reference; public string PaymentType; // date of transaction public string TransactionDate; public string Notes; // customer data goes here pu...

Can this code be refactored by using the reactive framework ?

Hi all, copy paste the following code in new C# console app. class Program { static void Main(string[] args) { var enumerator = new QueuedEnumerator<long>(); var listenerWaitHandle = Listener(enumerator); Publisher(enumerator); listenerWaitHandle.WaitOne(); } private static AutoResetEve...

Multiple enumerators for an IEnumerable

I have a custom made collection that has many modes of objects generations inside of it. It can generate everything, one object at a time or N objects at a time. I would like to have the option to switch between implementations of generation on runtime, and even maybe create new ones. I am looking for something with this kind of syntax: ...

A question about IEnumerator.GetEnumerator in C#

Hi there, I have a question about the IEnumerator.GetEnumerator() method. public class NodeFull { public enum Base : byte {A = 0, C, G, U }; private int taxID; private List<int> children; public int TaxID { get { return taxID; } set { taxID = value; } } public int this[int i] { g...

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

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 ...