Coming from here i have something equal in my code. Let's say i have the following class:
public class EList<T> : List<T>
{
public string Add { get; set; }
}
now i'm expecting the error
Ambiguity between 'EList.Add' and 'EList.Add(int)'
but the following code works without any errors:
EList<int> il = new EList<int>();
il.Add(1);
il.Add = "test";
Question: Does inheritance suppress this error by mistake?
It's explicitly catered for within the member lookup rules of C# (C# Specification section 7.4). Specifically, what matters here is invocability
skipping earlier rules
Next, if the member is invoked, all non-invocable members are removed from the set. (The call to
Add()
is invoked. Therefore theAdd
property is removed from the set)Next, members that are hidden by other members are removed from the set. For every member S.M in the set, where S is the type in which the member M is declared, the following rules are applied:
- If M is a constant, field, property, event, or enumeration member, then all members declared in a base type of S are removed from the set. (The
Add
method in the base type is removed)more rules
So at this point, no ambiguity since the above two rules have eliminated one or other of the potentially conflicting members.
See also section 10.3, Class members:
The name of a constant, field, property, event, or type must differ from the names of all other members declared in the same class.
The name of a method must differ from the names of all other non-methods declared in the same class. In addition ...
(My emphasis)
And,
The inherited members of a class type (§10.3.3) are not part of the declaration space of a class. Thus, a derived class is allowed to declare a member with the same name or signature as an inherited member
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