I’m writing a cache-eject method that essentially looks like this:
while ( myHashSet.Count > MAX_ALLOWED_CACHE_MEMBERS )
{
EjectOldestItem( myHashSet );
}
My question is about how Count is determined: is it just a private or protected int, or is it calculated by counting the elements each time its called?
From http://msdn.microsoft.com/en-us/library/ms132433.aspx:
This guarantees that accessing the
Countwon’t iterate over the whole collection.Edit: as many other posters suggested,
IEnumerable<...>.Count()is however not guaranteed to be O(1). Use with care!IEnumerable<...>.Count()is an extension method defined inSystem.Linq.Enumerable. The current implementation makes an explicit test if the countedIEnumerable<T>is indeed an instance ofICollection<T>, and makes use ofICollection<T>.Countif possible. Otherwise it traverses theIEnumerable<T>(possible making lazy evaluation expand) and counts items one by one.I’ve not however found in the documentation whether it’s guaranteed that
IEnumerable<...>.Count()uses O(1) if possible, I only checked the implementation in .NET 3.5 with Reflector.Necessary late addition: many popular containers are not derived from
Collection<T>, but nevertheless theirCountproperty is O(1) (that is, won’t iterate over the whole collection). Examples areHashSet<T>.Count(this one is most likely what the OP wanted to ask about),Dictionary<K, V>.Count,LinkedList<T>.Count,List<T>.Count,Queue<T>.Count,Stack<T>.Countand so on.All these collections implement
ICollection<T>or justICollection, so theirCountis an implementation ofICollection<T>.Count(orICollection.Count). It’s not required for an implementation ofICollection<T>.Countto be an O(1) operation, but the ones mentioned above are doing that way, according to the documentation.(Note aside: some containers, for instance,
Queue<T>, implement non-genericICollectionbut notICollection<T>, so they “inherit” theCountproperty only from fromICollection.)