小编ede*_*ane的帖子

如何禁止使用字段而不是属性?

我的类中有一个属性,在set访问器中有很多逻辑:

private String text;

public String Text
{
   get { return text; }
   private set
   {
      // some actions with a value
      value = value.Replace('a', 'b');

      text = value;
   }
}
Run Code Online (Sandbox Code Playgroud)

如何防止其他开发人员(甚至是我)更改此类中的字段而不是属性

如果有人写这样的话:

public Test(String text)
{
   this.text = text;
}
Run Code Online (Sandbox Code Playgroud)

它会打破我班级的逻辑!

c# properties

13
推荐指数
4
解决办法
897
查看次数

标签 统计

c# ×1

properties ×1