Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thumbrule on which collections have the Length or Count property - C#

Tags:

c#

.net

Some C# collections have the count and some of them have the length property. Is there a thumbrule to find out which one has which and why the discrepency?

like image 433
DotnetDude Avatar asked Dec 29 '22 04:12

DotnetDude


1 Answers

I'd say general Thumbrule would be the following:

  • Count is for collections with a variable length, i.e. Lists (from ICollection)
  • Length is for fixed length collections, i.e. Arrays, or other immutable objects, i.e. string.

UPDATE:

Just to elaborate Count comes through from ICollection and doesn't always indicate variability, for example (as per Greg Beech's comment) the ReadOnlyCollection<T> has the Count property but it is not variable, however it does implement ICollection.

Perhaps a more exact rule of thumb would be:

  • Count indicates that something implements ICollection
  • Length indicates immutability.
like image 176
djdd87 Avatar answered Feb 06 '23 23:02

djdd87