与C#中的getter和setter接口 [英] Interface with getter and setter in c#

查看:111
本文介绍了与C#中的getter和setter接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如我在这里阅读的 http://msdn.microsoft.com/en-us/library/75e8y5dd%28v=VS.100%29.aspx

可能在接口中有get,但不是set吗?

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

或者如果我想要在接口中使用getter和setter,是否必须仅使用新语法getVar setVar只是因为新语法不适合接口语法?

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?

更新:如果我必须在Interface中省略set,这是否意味着我不能强制类具有setter,而在这种情况下,由于只能部分执行,因此无法实现具有Interface的目的?

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?

推荐答案

否.我想你误会了.这篇文章是关于可能性的,该界面具有一个具有readonly属性(仅具有getter的属性)的接口.但是,如果需要,您也可以将setter放在界面中:

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; }
}

这篇关于与C#中的getter和setter接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆