Why doesn't generic ICollection implement IReadOnlyCollection in .NET 4.5?

后端 未结 6 839
北恋
北恋 2020-12-03 02:45

In .NET 4.5 / C# 5, IReadOnlyCollection is declared with a Count property:

public interface IReadOnlyCollection

        
6条回答
  •  盖世英雄少女心
    2020-12-03 03:03

    This interface is more a description interface then really functional thing.

    In suggested approach every collection must be understood as something that you can onlly read ad is always the same. So you could not add or remove a thing after creation.

    We could ask our self why the interface is called IReadOnlyCollection, not 'IReadOnlyEnumerable' as it use IEnumerable, IEnumerable. The answer is easy this type of interface would not have any senece, because Enumerable is already 'read only'.

    So lets look in doc IReadOnlyCollection(T)

    The first (and last) senetece in description says:

    Represents a strongly-typed, read-only collection of elements.

    And I think that this explain everything if you know what strong-typing is for.

提交回复
热议问题