ienumerator

C#: Can I have a method return IEnumerator<T> and use it in a foreach loop?

Howdy, I need to set the height of every textbox on my form, some of which are nested within other controls. I thought I could do something like this: private static IEnumerator<TextBox> FindTextBoxes(Control rootControl){ foreach (Control control in rootControl.Controls) { if (control.Controls.Count > 0) { ...

Why IEnumerator of T inherts from IDisposable, but non-generic IEnumerator does NOT?

I noticed that generic IEnumerator(of T) inherits from IDisposable, but the non-generic interface IEnumerator does NOT. Why it is designed in this way? Usually, we use foreach statement to go through a IEnumerator(of T) instance. The generated code of foreach actually has try-finally block that invokes Dispose() in finally. Edit: My m...

Is Yield Return == IEnumerable & IEnumerator?

I just want to verify, is yield return a shortcut for implementing IEnumerable and IEnumerator? Thanks, John ...

Implementing a bidirectional enumerator in C#

Is there a way to use yield blocks to implement an IEnumerator<T> which can go backward (MoveLast()) as well as forward? ...

IEnumerable<T> provides two GetEnumerator methods - what is the difference between them?

When I emplement IEnumerable<T> interface I see two GetEnumerator methods: one returning IEnumerator and other IEnumerator<T>. When would I use one or another? ...

What is the difference between IEnumerator and IEnumerable?

What are the differences between IEnumerator and IEnumerable? ...

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

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

C#: IEnumerator<T> in a using statement

I was curious to see how the SingleOrFallback method was implemented in MoreLinq and discovered something I hadn't seen before: public static T SingleOrFallback<T>(this IEnumerable<T> source, Func<T> fallback) { source.ThrowIfNull("source"); fallback.ThrowIfNull("fallback"); using (IEnumerator<T> iterator...

C# : obtain generic enumerator from an array

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

.NET: is there a "HasNext" method for an IEnumerator ?

With Java Iterators, 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 IEnumerators? ...

How do I select a subset of items from an anonymous type IEnumerable?

I have the following code. MyDataContext db = MyDataContext.Create(); bc = db.BenefitCodes.Select( b => new { BenCd = b.BenCd , Description = b.BenDesc , BenInter...

Calling IEnumerator from an Interface class

I am new to enumerating collections so forgive if this question sounds silly. I have an class public class PersonStuff : IPersonStuff, IEnumerable<User> { Person person = new Person(); ... IEnumerator<Person> IEnumerable<Person>.GetEnumerator() { return (person as IEnumerable<Person>).Ge...

Enumeration of .Net IList

I'd like to know if I can assume that the IEnumerator I get from an IList (by calling the GetEnumerator method from the IEnumerable interface) will give me the items in the order of the list. What do you think? ...

C#: Where to implement a custom IEnumerator<T>

Say I have a class that implements IEnumerable<T>. It currently uses the yield keyword in the GetEnumerator() method. But now I need to do a bit more, for example I would like to clean up after myself. To do this, unless I have overlooked anything, I need to implement the IEnumerator<T> interface. But where would you say I should do that...

Convert CollectionBase to List or data type usable with Linq

Hi I am using Aspose cells to manipulate Excel spreadsheets. One of the types in the API is a collection of Pictures in the spreadsheet, which derives from CollectionBase: see this link: http://www.aspose.com/documentation/.net-components/aspose.cells-for-.net/aspose.cells.pictures.html I want to convert this type to something that al...

Is it possible to get an IEnumerator<T> from a T[]?

Let's say I want to create a collection class that is thread-safe by default. Internally, the class has a protected List<T> property called Values. For starters, it makes sense to have the class implement ICollection<T>. Some of this interface's members are quite easy to implement; for example, Count returns this.Values.Count. But imp...

Why doesn't .NET have a bidirectional enumerator?

It's been asked a couple of times on SO how you can implement a bidirectional enumerator (http://stackoverflow.com/questions/191788/two-directional-list-enumerator-in-net, http://stackoverflow.com/questions/451099/implementing-a-bidirectional-enumerator-in-c). My question is not how (which is trivial for most cases), but why no such type...

How to access index in IEnumerator object in C#?

Hi there I have an iEnumerator object. I would like to access based on index for instance: for(i=0; i<=Model.Products; i++) { ??? } Is this possible? ...

Return an empty IEnumerator

Hello! 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 GetEnumerat...

Looking for a faster implementation for IEnumerable/IEnumerator

I'm trying to optimize a concurrent collection that tries to minimize lock contention for reads. First pass was using a linked list, which allowed me to only lock on writes while many simultaneous reads could continue unblocked. This used a custom IEnumerator to yield the next link value. Once i started comparing iteration over the colle...