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?
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.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With