且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

属性的获取/设置方法上可能有不同的访问修饰符吗?证明合法

更新时间:2022-12-17 10:52:05

是的-但是所有访问修饰符都在声明该项时设置,此后不能更改.

不同的修饰符对于属性特别有用,因为它们允许外界对变量的访问受到限制.

例如,您可能具有字符串属性:
Yes - but all access modifiers are set when you declare the item and cannot be changed after that.

Different modifiers are particularly useful with properties, as they allow the outside world to have limited access to your variables.

For example, you might have a string property:
public string Text { get; private set; }

外部世界可以随时读取您正在处理的文本,但是只有创建该属性的类才可以对其进行修改.

Where the outside world can read the text you are processing at any time, but only the class that created the property can ever modify it.