Recently a new learner of C# asked me a question on C# Virtual & Abstract properties. It turned out to a deep dive. Detail follows.
Difference between the Abstract and Virtual property
* Abstract properties forces you to make the class abstract,
no such constraint for virtual properties. Virtual provides
an offer to subclass to override.
* Abstract properties defer the implementation to subclass.
In Virtual property a default implementation can be provided..
Let us deep dive on write only virtual property
-------------------------------------------------------------
To make a property as write only
Option 1: You can mark the getter as private
public virtual string Prop1 { set; private get; }
Option 2: You can just have setter
> in case of explicit implemented properties,
it is legal to just have Setter
/////////////////////////////////////////////////////
string Field;
public virtual string Prop1 { set { Field = value; } }
/////////////////////////////////////////////////////
> in case of Automatically implemented properties, you must have getter
because if you have just the setter, there is no way you can
access the value. (Check compiler error below)
/////////////////////////////////////////////////////
public virtual string Prop1 { set; }
/////////////////////////////////////////////////////
Error:
'ConsoleApplication1.test.MyVirtaulWriteOnlyProperty1.set' must declare a body
because it is not marked abstract or extern. Automatically implemented properties
must define both get and set accessors.
Detail:
So for virtual write only property, you must explicitly have setter implemented.
if you are using Automatically implemented property it must have both get & set
In contrary write only abstract property allows setter only property.
It is because, in abstract the implementation is deferred, in the subclass
the compiler will force you to implement the setter explicitly in subclass.
/////////////////////////////////////////////////////
abstract class Animal
{
public abstract string Prop1 { set; }
}
/////////////////////////////////////////////////////
Attempt - 1
/////////////////////////////////////////////////////
public class Dog : Animal
{
public override string Prop1 {set;}
}
/////////////////////////////////////////////////////
Errror
'ConsoleApplication1.Dog.Prop1.set' must declare a body
because it is not marked abstract or extern.
Automatically implemented properties must define both get and set accessors.
Detail:
The reason is there is no way you can read the value,
so compiler forces us to add both get, set.
Attempt - 2
/////////////////////////////////////////////////////
public class Dog : Animal
{
public override string Prop1 {get;set;}
}
/////////////////////////////////////////////////////
Error:
'ConsoleApplication1.Dog.Prop1.get': cannot override
because 'ConsoleApplication1.Animal.Prop1'
does not have an overridable get accessor
Detail:
There is no getter defined in suberclass, so we cannot define it in subclass.
We cannot change the write only property as read/write property in subclass
Attempt - 3: (Correct implementation)
/////////////////////////////////////////////////////
public class Dog : Animal
{
string field;
public override string Prop1
{
set { field = value; }
}
}
/////////////////////////////////////////////////////
Error: None
Detail:
So the only way out is implement the Write only property as write only explicitly.
I hope this helps to understand the write only property behavior in reference to Abstract & Virtual.