我应该何时使用readonly /省略属性上的set accessor / private accessor [英] When should I use readonly / omit the set accessor / private accessor on a property

查看:64
本文介绍了我应该何时使用readonly /省略属性上的set accessor / private accessor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello

我应该何时使用readonly属性/省略属性上的set accessor / private accessor

When should I use readonly property / omit the set accessor / private accessor on a property

请解释

问候

Manoj

推荐答案

如果要在构造函数中设置值,请使用readonly,并且在以后值不能更改那。因此,类的每个实例都可以具有不同的属性值,但是该值在设置后不能更改(与const不同,其中值为
,对于所有实例都相同)。

Use readonly when you want to set the value in the constructor, and the value can't change after that. So each instance of the class can have a different value for the property, but that value can't change once set (unlike a const, where the value is the same for all instances).

当类的实例本身可能更改属性的值时,省略set访问器,但其他类不能更改它(尽管其他类可以读取它)。

Omit the set accessor when the instance of the class might itself change the value of the property, but other classes can not change it (although other classes can read it).

当班级外的任何人都无法改变或阅读财产时,请使用私人访问者。

Use the private accessor when nothing outside the class can change or read the property.


这篇关于我应该何时使用readonly /省略属性上的set accessor / private accessor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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