Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

IEnumerable Item Count

0 views
Skip to first unread message

Oliver Gargour

unread,
Jan 27, 2006, 5:56:37 PM1/27/06
to
Is at all possible to get IEnumerable Item Count without having to loop
through using GetEnumerator() ??
Thanks


chris martin

unread,
Jan 27, 2006, 6:05:33 PM1/27/06
to

<see langword="true" /> if the object implementing IEnumberable has a Count
property; otherwise <see langword="false" />.

Wow. That's even geeky for me.. ;)


Jon Skeet [C# MVP]

unread,
Jan 27, 2006, 6:14:30 PM1/27/06
to
Oliver Gargour <newsg...@garwin.be> wrote:
> Is at all possible to get IEnumerable Item Count without having to loop
> through using GetEnumerator() ??

Not in general. There may not even be a sensible meaning to that. For
instance, it would be possible (potentially useful even) to write an
IEnumerable implementation on top of TextReader, so you could do:

foreach (string line in new LineReader(myTextReader))
{
}

Now, potentially that TextReader is coming over a network connection
which may keep giving text forever.

Alternatively, a random number generator could implement IEnumerable -
again, no sensible concept of count.

--
Jon Skeet - <sk...@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Nick Hounsome

unread,
Jan 28, 2006, 5:20:24 AM1/28/06
to

"chris martin" <chris_m|NOSPAM|@caliber|SPAM|web.com> wrote in message
news:441fa36b2712c8...@news.easynews.com...

Check to see whether the object implements ICollection (which has a Count
property).

If a class is IEnumerable and has a Count then I can't think of a good
reason why it would not implement ICollection since ICollection =
IEnumerable + Count + a few methods that MUST be implementable.

Better yet overload your method with one taking ICollection


0 new messages