Question

Is it possible to do something like this?

class A
{
    public virtual string prop
    {
        get
        {
            return "A";
        }
    }
}
class B: A
{
    private string X;
    public override string prop
    {
        get
        {
            return X;
        }
        set
        {
            X = value;
        }
    }
}

That is, the base class provides a virtual property with only a GET accessor, but the child class overrides the GET and also provides a SET.

The current example doesn't compile, but perhaps I'm missing something here.

Added: To clarify, no I don't want to redefine with new. I want to add a new accessor. I know it wasn't in the base class, so it cannot be overriden. OK, let me try to explain how it would look without the syntactic sugar:

class A
{
    public virtual string get_prop()
    {
            return "A";
    }
}
class B: A
{
    private string X;
    public override string get_prop()
    {
        return X;
    }
    public virtual string set_prop()
    {
        X = value;
    }
}
Was it helpful?

Solution

No, this is not possible. Unfortunately you can not even override and change the accessor level (from protected to public for example), as documented on MSDN. I would recommend that you consider restructuring the code/class slightly and look for an alternative way to accomplish this task such as declaring the set accessor with the protected modifier using a SetProperty method in the derived class.

OTHER TIPS

No, there is no way to do this. Think about how the syntactic sugar of your virtual property is being dealt with, i.e. it gets converted to:

public virtual string get_prop();

There is no set_Prop method to override, and you can't override a method that doesn't exist.

You can hide the base class implementation by using the 'new' keyword in your derived class. The following should compile succesfully:

class A
{
    public virtual string prop
    {
        get
        {
            return "A";
        }
    }
}
class B : A
{
    private string X;
    public new string prop
    {
        get
        {
            return X;
        }
        set
        {
            X = value;
        }
    }
}

Introducing a new field smacks of messy. And either way you need to be careful not to break polymorphism and inheritance (what if the base-class talks to the private field?).

With regard to adding a new accessor while overriding; the simple answer is "no, you can't". When you override, you can only impact the existing accessors, since that is what is defined in the virtual-table for the member (an overridden member is still "owned" by the base/declaring class, not the overriding class).

One option (not ideal) is to re-declare the property, but you would still need a way to talk down to the base-class. So if the accessor in the base-class was protected:

class A
{
    public string prop
    {
        get;
        protected set;
    }
}
class B : A
{
    public new string prop
    {
        get { return base.prop; }
        set { base.prop = value; }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top