Interface with getter and setter in c#

user310291 picture user310291 · Sep 20, 2010 · Viewed 31.7k times · Source

As I read here http://msdn.microsoft.com/en-us/library/75e8y5dd%28v=VS.100%29.aspx

It is possible to have get in an Interface BUT NOT set ?

OR if I want getter and setter in Interface, do I have to use the old syntax getVar setVar just because new syntax doesn't fit Interface syntax?

Update: If I must omit set in Interface, does this means I cannot enforce class to have setter which defeats the purpose of having an Interface in this case as I can only partially enforce?

Answer

Andrea Parodi picture Andrea Parodi · Sep 20, 2010

No. I think you misunderstood. That article is about the possibility of having an interface with a readonly property (a property with only getter). But, if you need, you can put also the setter in the interface:

interface IHasProperty
{
    string Property{ get;set; }
}
class HasProperty:IHasProperty 
{
    public string Property{ get;set; }
}