英文:
Is it possible to both override AND hide a property from a base class, and then have one implemented in terms of the other?
问题
I've found a bug in our code, where we have a class which inherits from System.Data.Common.DbParameter
, and it hides the Precision
& Scale
properties by declaring them to return short
(instead of byte
like in the base class).
To avoid a breaking change, I'd like to leave the short
properties there, but then override the byte
properties to be implemented in terms of the short
ones (not going to worry too much about what happens when there's an overflow on get/set for now, since in practice the values should fit in a byte
)
But I'm not sure how to actually do that, syntactically. When I try to do
public override byte Precision
{
get
{
return <ClassName>.Precision;
}
}
public new short Precision
{
// stuff
}
It seems like it's still referring to the byte
version of the property? How do I explicitly refer to the short
version?
Edit: After fiddling a bit, it's giving an error directly on the second declaration of the property, that it's already been defined, so not sure if they can even be both defined in the same class?
英文:
I've found a bug in our code, where we have a class which inherits from System.Data.Common.DbParameter
, and it hides the Precision
& Scale
properties by declaring them to return short
(instead of byte
like in the base class).
To avoid a breaking change, I'd like to leave the short
properties there, but then override the byte
properties to be implemented in terms of the short
ones (not going to worry too much about what happens when there's an overflow on get/set for now, since in practice the values should fit in a byte
)
But I'm not sure how to actually do that, syntactically. When I try to do
public override byte Precision
{
get
{
return <ClassName>.Precision;
}
}
public new short Precision
{
// stuff
}
It seems like it's still referring to the byte
version of the property? How do I explicitly refer to the short
version?
Edit: After fiddling a bit, it's giving an error directly on the second declaration of the property, that it's already been defined, so not sure if they can even be both defined in the same class?
答案1
得分: 0
No, a class cannot both override and hide a property. That implies that the class is declaring two properties with the same name, which is illegal. From the language spec:
> The name of a constant, field, property, or event shall differ from the names of all other members declared in the same class.
One way to work around this is by adding an intermediate class in the inheritance hierarchy. Suppose your class YourClass
inherits from SomeOtherClass
, you can add an Intermediate
class to put your override implementation in, like this:
class Intermediate: SomeOtherClass {
public override byte Precision {
get {
// implement this...
}
}
}
class YourClass: Intermediate {
public new short Precision => ((Intermediate)this).Precision;
}
英文:
No, a class cannot both override and hide a property. That implies that the class is declaring two properties with the same name, which is illegal. From the language spec:
> The name of a constant, field, property, or event shall differ from the names of all other members declared in the same class.
One way to work around this is by adding an intermediate class in the inheritance hierarchy. Suppose your class YourClass
inherits from SomeOtherClass
, you can add an Intermidate
class to put your override implementation in, like this:
class Intermediate: SomeOtherClass {
public override byte Precision {
get {
// implement this...
}
}
}
class YourClass: Intermediate {
public new short Precision => ((Intermediate)this).Precision;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论