c# - Why is a property hiding Inherited function with same name? -
i have class foo function myname in it.
class bar inherits foo , have property called myname
i figured should ok since:
- the property compile
get_myname()(so there not collisionfoo::myname(int,int)) foo::mynamehas arguments while, obviously, property not (again, no colision).
yet still warning:
'bar.myname' hides inherited member 'foo.myname(int, int)'. use new keyword if hiding intended. example code:
public class foo { public int myname(int a, int b) { return 0; } } class bar: foo { int a; int b; int myname { { return + b; } } } also, if add bar function:
int get_myname() { return a+b; } as expected, error function declared.
happening here? property not allowing me use overloads of myname in addition get_myname()?
section 10.3.3 of c# spec contains:
a derived class can hide inherited members declaring new members same name or signature.
it doesn't 2 members having same kind of member. note though 1 property , 1 method, there still cases confused:
- the property of delegate type, e.g.
action<int, int>in casepropertyname(10, 10)valid. - in places use property name, trying perform method group conversion, they'd both valid.
within same class, run rules of section 10.3:
- the names of constants, fields, properties, events, or types must differ names of other members declared in same class.
- the name of method must differ names of other nonmethods declared in same class. [...]
the fact can't declare method called get_myname mentioned in section 10.3.9.1, noted in comments.
Comments
Post a Comment