Setting default value for interface properties?

I have an interface that contains one property. I need to set a default value for this property. How to do it?. Is it also good to have defualt for a property in an interface? or here, using an abstract class, is apt instead?

Thanx in advance

+4
source share
2 answers

You cannot set a default value for an interface property.

Use an abstract class in addition to the interface (which only sets the default and does not implement anything):

public interface IA { int Prop { get; } void F(); } public abstract class ABase : IA { public virtual int Prop { get { return 0; } } public abstract void F(); } public class A : ABase { public override void F() { } } 
+6
source

Interfaces do not contain implementations. All they do is subscribe to state membership.

An interface implementation can have any default value that it likes for any property.

eg. An abstract class can return a default value for any of its properties.

+1
source

All Articles