Is it possible to both override AND hide a property from a base class, and then have one implemented in terms of the other?

huangapple go评论60阅读模式
英文:

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 &lt;ClassName&gt;.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 =&gt; ((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 =&gt; ((Intermediate)this).Precision;
}

huangapple
  • 本文由 发表于 2023年5月18日 06:54:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76276682.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定