不是私人更多的私人? (C#) [英] More private than private? (C#)

查看:136
本文介绍了不是私人更多的私人? (C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时候,你有一个备份属性的私人领域,您只想通过属性setter设置字段,以便进一步处理可以做到无论何时领域的变化。问题是,它仍然容易不小心从同一类的其他方法中绕过属性setter并没有注意到,你已经这样做了。有没有在C#的方式来解决这个或一般的设计原则,以避免它?

Sometimes you have a private field that backs a property, you only ever want to set the field via the property setter so that additional processing can be done whenever the field changes. The problem is that it's still easy to accidentally bypass the property setter from within other methods of the same class and not notice that you've done so. Is there a way in C# to work around this or a general design principle to avoid it?

推荐答案

恕我直言,这是不使用,这是因为:

IMHO, it is not used, because:


  • 的类必须信任本身

  • 如果您的类获取大的一部分并不知道其他的,应当划分。

  • 如果属性背后的逻辑稍微复杂一些,考虑到封装它在自己的类型。

这篇关于不是私人更多的私人? (C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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