Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override abstract readonly property to read/write property

I would like to only force the implementation of a C# getter on a given property from a base abstract class. Derived classes might, if they want, also provide a setter for that property for public use of the statically bound type.

Given the following abstract class:

public abstract class Base {     public abstract int Property { get; } } 

If I want a derived class that also implements a setter, I could naively try:

public class Derived : Base {     public override int Property     {         get { return field; }         set { field = value; } // Error : Nothing to override.     }       private int field; } 

But then I get a syntax error since I try to override the non existing setter. I tried some other way such as declaring the base setter private and such and I still stumble upon all kind of errors preventing me from doing that. There must be a way to do that as it doesn't break any base class contract.

Incidentaly, it can be done with interfaces, but I really need that default implementation.

I stumbled into that situation so often, I was wondering if there was a hidden C# syntax trick to do that, else I will just live with it and implement a manual SetProperty() method.

like image 907
Coincoin Avatar asked Sep 28 '09 21:09

Coincoin


People also ask

Can we override a property in C#?

In C# 8.0 and earlier, the return types of an override method and the overridden base method must be the same. You cannot override a non-virtual or static method. The overridden base method must be virtual , abstract , or override . An override declaration cannot change the accessibility of the virtual method.

Can properties be overridden?

You cannot "override" fields, because only methods can have overrides (and they are not allowed to be static or private for that). This trick lets A 's subclasses "push" a new value into the context of their superclass, getting the effect similar to "overriding" without an actual override.

What is abstract property in C#?

An abstract property declaration does not provide an implementation of the property accessors -- it declares that the class supports properties, but leaves the accessor implementation to derived classes. The following example demonstrates how to implement the abstract properties inherited from a base class.

Can we have properties in abstract class C#?

An abstract class cannot be instantiated. An abstract class not only contains abstract methods and assessors but also contains non-abstract methods, properties, and indexers. It is not possible to modify an abstract class with the sealed modifier because the two modifiers have opposite meanings.


1 Answers

You can't do it directly, since you can't new and override with the same signature on the same type; there are two options - if you control the base class, add a second property:

public abstract class Base {     public int Property { get { return PropertyImpl; } }     protected abstract int PropertyImpl {get;} } public class Derived : Base {     public new int Property {get;set;}     protected override int PropertyImpl     {         get { return Property; }     } } 

Else you can introduce an extra level in the class hierarchy:

public abstract class Base {     public abstract int Property { get; } } public abstract class SecondBase : Base {     public sealed override int Property     {         get { return PropertyImpl; }     }     protected abstract int PropertyImpl { get; } } public class Derived : SecondBase {     public new int Property { get; set; }      protected override int PropertyImpl     {         get { return Property; }     } } 
like image 156
Marc Gravell Avatar answered Sep 23 '22 10:09

Marc Gravell