IssueRaiser

Make mutable generic collection interfaces implement read-only collection interfaces

## Rationale It's long been a source of confusion that the mutable generic collection interfaces don't implement their respective read-only collection interfaces. This was of course due to the read-only collection interfaces being added after the fact and thus would cause breaking changes by changing a published interface API. With the addition of default interface implementations in C#8/.NET Core 3.0 I think the mutable generic collection interfaces, `ICollection<T>`, `IList<T>`, `IDictionary<K, V>`, and `ISet<T>` should now implicitly inherit their respective read-only collection interfaces. This can now be done without causing breaking changes. While it would have been nice for these interfaces to share members, I think the proposed API below is the best we can possibly do with the read-only interfaces being added after the fact. As an added bonus, this should allow some simplification of the type checking in LINQ code to check for the read-only interfaces instead of the mutable interfaces. ## Proposed API ```diff namespace System.Collections.Generic { - public interface ICollection<T> : IEnumerable<T> { + public interface ICollection<T> : IReadOnlyCollection<T> { - int Count { get; } + new int Count { get; } + int IReadOnlyCollection<T>.Count => Count; } - public interface IList<T> : ICollection<T> { + public interface IList<T> : ICollection<T>, IReadOnlyList<T> { - T this[int index] { get; set; } + new T this[int

Posted by TylerBrinkley on Sep 27, 2019, 8:26 PM

Tags

Related issues

Raise real-world problems, find people facing the same issue, and discuss solutions.3,500 issues0 comments

github (EdTech)

Make mutable generic collection interfaces implement read-only collection interfaces

TylerBrinkleySep 27, 2019, 8:26 PM

## Rationale It's long been a source of confusion that the mutable generic collection interfaces don't implement their respective read-only collection interfaces. This was of course due to the read-only collection interfaces being added after the fact and thus would cause breaking changes by changing a published interface API. With the addition of default interface implementations in C#8/.NET Core 3.0 I think the mutable generic collection interfaces, `ICollection<T>`, `IList<T>`, `IDictionary<K, V>`, and `ISet<T>` should now implicitly inherit their respective read-only collection interfaces. This can now be done without causing breaking changes. While it would have been nice for these interfaces to share members, I think the proposed API below is the best we can possibly do with the read-only interfaces being added after the fact. As an added bonus, this should allow some simplification of the type checking in LINQ code to check for the read-only interfaces instead of the mutable interfaces. ## Proposed API ```diff namespace System.Collections.Generic { - public interface ICollection<T> : IEnumerable<T> { + public interface ICollection<T> : IReadOnlyCollection<T> { - int Count { get; } + new int Count { get; } + int IReadOnlyCollection<T>.Count => Count; } - public interface IList<T> : ICollection<T> { + public interface IList<T> : ICollection<T>, IReadOnlyList<T> { - T this[int index] { get; set; } + new T this[int

Comments (0)

No comments yet. Start the discussion.

Comment on this issue