471,319 Members | 1,469 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,319 software developers and data experts.

IEnumerable Item Count

Is at all possible to get IEnumerable Item Count without having to loop
through using GetEnumerator() ??
Thanks
Jan 27 '06 #1
3 19546
> Is at all possible to get IEnumerable Item Count without having to
loop
through using GetEnumerator() ??
Thanks


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

Wow. That's even geeky for me.. ;)
Jan 27 '06 #2
Oliver Gargour <ne********@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
Jan 27 '06 #3

"chris martin" <chris_m|NOSPAM|@caliber|SPAM|web.com> wrote in message
news:44**************************@news.easynews.co m...
Is at all possible to get IEnumerable Item Count without having to
loop
through using GetEnumerator() ??
Thanks


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

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


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
Jan 28 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Pete | last post: by
3 posts views Thread by phil | last post: by
2 posts views Thread by Ash Jones | last post: by
1 post views Thread by jez123456 | last post: by
3 posts views Thread by mkiger | last post: by
5 posts views Thread by Tin Gherdanarra | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.