Can anyone explain why properties cannot directly extend an accessor when inheriting? #7174
-
Actually, what I want to ask is, why does IList not inherit IReadonlyList. |
Beta Was this translation helpful? Give feedback.
Replies: 8 comments 29 replies
-
Because then someone could downcast an IReadOnlyList to an IList and mutate it, violating the contract. |
Beta Was this translation helpful? Give feedback.
-
I'm not sure what this means. |
Beta Was this translation helpful? Give feedback.
-
Because interfaces offer an OOP concept for "can do" relationship.
So, I'll tell you why Suppose if you can design APIs for C#, please consider: what members can be defined for a read-only list? Obviously, a read-only list can only be iterated and fetched internal elements. This is what members that There's a coincidence for that indexer: both In short, use just a sentence to conclude: You cannot make If you want to do the same thing you've considered, just change to another way to approach. |
Beta Was this translation helpful? Give feedback.
-
If I were to do it, I would make it this way internal interface IReadOnlyCollection<out T> : IEnumerable<T>, IEnumerable
{
int Count { get; }
bool IsReadOnly { get; }
}
internal interface ICollection<T> : IReadOnlyCollection<T>
{
void Add(T item);
void Clear();
bool Contains(T item);
void CopyTo(T[] array, int arrayIndex);
bool Remove(T item);
}
internal interface IReadOnlyList<out T> : IReadOnlyCollection<T>
{
T this[int index] { get; }
}
internal interface IList<T> : ICollection<T>, IReadOnlyList<T>
{
T IReadOnlyList<T>.this[int index] => this[index];
new T this[int index] { get; set; }
int IndexOf(T item);
void Insert(int index, T item);
void RemoveAt(int index);
} The problem with this design is that it has two indexer. |
Beta Was this translation helpful? Give feedback.
-
For the design issue of IReadOnlyList, let's rethink why we should use it. internal class Foo
{
private IList<int> list;
public Foo(IList<int> list)
{
this.list = list;
}
}
internal class Boo
{
private IReadOnlyList<int> list;
public Boo(IReadOnlyList<int> list)
{
this.list = list;
}
} But how do I convert IList to IReadOnlyList? public Foo(IList<int> list)
{
this.list = list;
Boo boo = new Boo(list.AsReadOnly());
} Is this what we want? |
Beta Was this translation helpful? Give feedback.
-
@zms9110750 it's unclear what you'd like the language to do here. IList/IReadOnlyList aren't part of C#, they are part of the BCL. |
Beta Was this translation helpful? Give feedback.
-
A historical fact: the readonly interfaces (.NET Framework 4.5) comes way after non-readonly generic interfaces (.NET Framework 2.0). "Moving" the declaration of interface member to base interface is a breaking change, since it will break explicit interface implementation. If we redesign the interfaces today, the read-write interfaces will probably inherit from corresponding readonly ones. |
Beta Was this translation helpful? Give feedback.
-
Do some people believe that there should be a restriction on a class or interface that cannot achieve something? |
Beta Was this translation helpful? Give feedback.
A historical fact: the readonly interfaces (.NET Framework 4.5) comes way after non-readonly generic interfaces (.NET Framework 2.0).
"Moving" the declaration of interface member to base interface is a breaking change, since it will break explicit interface implementation. If we redesign the interfaces today, the read-write interfaces will probably inherit from corresponding readonly ones.