Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interface with getter and setter in c#

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?

like image 430
user310291 Avatar asked Sep 20 '10 18:09

user310291


People also ask

CAN interface have getters and setters?

Technically the class that implements the interface is free to use a property or a getter. In the same way there is no way for us to specify that a property is a setter in an interface, but we can still use a setter in our class.

CAN interface have getters and setters in C#?

You CAN use both get and set in interface property!

How do you use setters and getters in two different classes?

To fix this, you need to pass a reference to the GetterAndSetter instance from class A to B . You can do this e.g. by passing it as a parameter to a method of B , or by creating a new instance of A in B and calling a method that provides an instance of GetterAndSetter .

What is setter and getter in C?

The getter function is used to retrieve the variable value and the setter function is used to set the variable value. Remember: You can directly access public member variables, but private member variables are not accessible. Therefore, we need getter functions.


1 Answers

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; }
}
like image 147
Andrea Parodi Avatar answered Oct 19 '22 03:10

Andrea Parodi