HCL
HCL

Reputation: 36775

Setter for a clr-property that is defined in an abstract base class with only a getter

Is there a way to declare a setter for a clr-property that is defined in an abstract base class with only a getter (and vice versa)?

abstract class BaseClass {
    public abstract string Test {
        get;
    }
}

class ConcreteClass : BaseClass{
    public override string Test {
        get { return string.Empty; }
        set { /* Some code*/} // This would be really pratically
    }
}

The same quesion may be asked for properties marked as virtual.

Upvotes: 2

Views: 425

Answers (2)

corvuscorax
corvuscorax

Reputation: 5940

There is a sort of a workaround possible.

Declare a protected setter in the base class, then implement it in the concrete classes.

Like this:

abstract class Base
{
    public abstract string Test { get; protected set; }

}

class Concrete : Base
{
    string s;
    public override string Test
    {
        get { return s; }
        protected set { s = value; }
    }
}

... but pretty, it ain't :-)

Upvotes: 2

leppie
leppie

Reputation: 117220

That is fortunately not possible. You cannot change an existing definition/contract.

There are ways around it, like the new keyword. Or using an interface.

Upvotes: 4

Related Questions