Using Explicit Interfaces to prevent accidental modification of properties in C#

跟風遠走 提交于 2019-12-06 09:54:13
Marc Gravell

That is perfectly well defined; the explicit interface implementations take priority when using the interface, and the regular property takes effect otherwise (including from within the body of the get/set).

As for making it tidier... the best I can offer is to reformat it to make it less verbose...

DateTime IHaveUpdateDateFields.CreatedOn
{
    get { return CreatedOn; }
    set { CreatedOn = value; }
}

(note also that the this is implicit and redundant)

As an aside - the safety is only a convenience, not a guarantee... external callers can still use your interface, and can usually (ab)use reflection to jump past mere things like protected - or even just set the fields directly.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!