Collections + Count property


Do I want a Count property on my collections?  If so, it seems like I’m fundamentally limiting my collections to represent collections of finite elements.  What if i want something that represents the set of all rational numbers?  ISet<Rational> would seem the best candidate.  However, then there isn’t a valid value for Count to return.  I could have it try returning -1, however, that seems quite ugly and seems like it might definitely confuse people.  I could also have it return an IOptional<int> (see http://weblogs.asp.net/jaybaz_ms/archive/2004/05/06/127693.aspx) which could return a None<int> to indicate that this collection had no count.  hrmmm… I thin for now I won’t include a count as it just seems too limiting.


Comments (2)

  1. Ron says:

    I would argue *for* the Count property. I would expect the majority of implementations to use a finite set of elements. If necessary, have it return a Nullable<int> and set the value to null for those collections with an infinite number of elements.

  2. Ron, i definitely agree with you (note that IOptional is very similar to Nullable). I’ll update you on my thoughts on this when I get to that point.