Generic enumerator on arrays

  • Thread starter Thread starter Douglas Harber
  • Start date Start date
D

Douglas Harber

If I declare an array:

SomeClass[] instancesOfSomeClass;

which has an array of SomeClass objects assigned to it, is there anyway to
use a generic enumerator with this collection?

I'd like to use an IEnumerator<SomeClass> but there doesn't appear to be any
way to generate such an enumerator on arrays.

Am I missing something?

Thanks,
Doug Harber
 
Douglas,

Arrays of type T implement IEnumerable<T>, so you can do this:

// Your array.
SomeClass[] instancesOfSomeClass;

// Get IEnumerable<SomeClass>
IEnumerable<SomeClass> enumeration = instancesOfSomeClass;

// Get IEnumerator<SomeClass>
IEnumerator<SomeClass> enumerator = enumeration.GetEnumerator();

Hope this helps.
 
Ah...that's very helpful. I was trying to cast the enumerator
(unsuccessfully). Didn't think to cast the collection itself to get an
enumerator.

Thanks, much.

Doug Harber

Nicholas Paldino said:
Douglas,

Arrays of type T implement IEnumerable<T>, so you can do this:

// Your array.
SomeClass[] instancesOfSomeClass;

// Get IEnumerable<SomeClass>
IEnumerable<SomeClass> enumeration = instancesOfSomeClass;

// Get IEnumerator<SomeClass>
IEnumerator<SomeClass> enumerator = enumeration.GetEnumerator();

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Douglas Harber said:
If I declare an array:

SomeClass[] instancesOfSomeClass;

which has an array of SomeClass objects assigned to it, is there anyway
to use a generic enumerator with this collection?

I'd like to use an IEnumerator<SomeClass> but there doesn't appear to be
any way to generate such an enumerator on arrays.

Am I missing something?

Thanks,
Doug Harber
 
Back
Top