Redian新闻
>
那位大侠能介绍一下 property in c# class的使用经验
avatar
那位大侠能介绍一下 property in c# class的使用经验# DotNet - 窗口里的风景
a*y
1
什么时候会用到呢?有什么好处?
avatar
p*n
2
可以用来实现比较严密的封装,控制外界与类的FIELD的接触.
实际上,不管是在C++还是在JAVA(e.g.,Bean)里头,用getter/setter 函数
(accessor,mutator)来控制外界读写的方式一经很常用了,C#只不过是在语
言里直接把这个常用的模式正式吸纳而已.
譬如,如果你想让一个属性只读, 就只定义一个GET好了. 很方便. 而且如果
你想改变属性的名称, 只用在一个地方改就行了.(呵呵, 如果在JAVA里摊上
个getter/setter 离好几百行的时候, 够费眼的.)

【在 a********y 的大作中提到】
: 什么时候会用到呢?有什么好处?
avatar
C*n
3
OK.
The real world best pratice:
1. Never declare public field. All the fields should be private.
2. Declare a property for every field.
3. In property, initialize field value if necessary.
That's how/when you use property.
Some often used pattern:
private string _connectionString = null;
public string ConnectionString
{
get
{
if (_connectionString == null || _connectionString == "")
_connectionString =
ConfigurationSettings.AppSetting["ConnectionString"];
return _conn

【在 p***n 的大作中提到】
: 可以用来实现比较严密的封装,控制外界与类的FIELD的接触.
: 实际上,不管是在C++还是在JAVA(e.g.,Bean)里头,用getter/setter 函数
: (accessor,mutator)来控制外界读写的方式一经很常用了,C#只不过是在语
: 言里直接把这个常用的模式正式吸纳而已.
: 譬如,如果你想让一个属性只读, 就只定义一个GET好了. 很方便. 而且如果
: 你想改变属性的名称, 只用在一个地方改就行了.(呵呵, 如果在JAVA里摊上
: 个getter/setter 离好几百行的时候, 够费眼的.)

avatar
L*r
4
还有一点,如果你用VS.Net可以很方便的显示和管理他们。

【在 p***n 的大作中提到】
: 可以用来实现比较严密的封装,控制外界与类的FIELD的接触.
: 实际上,不管是在C++还是在JAVA(e.g.,Bean)里头,用getter/setter 函数
: (accessor,mutator)来控制外界读写的方式一经很常用了,C#只不过是在语
: 言里直接把这个常用的模式正式吸纳而已.
: 譬如,如果你想让一个属性只读, 就只定义一个GET好了. 很方便. 而且如果
: 你想改变属性的名称, 只用在一个地方改就行了.(呵呵, 如果在JAVA里摊上
: 个getter/setter 离好几百行的时候, 够费眼的.)

avatar
a*y
5
Thx.

【在 C****n 的大作中提到】
: OK.
: The real world best pratice:
: 1. Never declare public field. All the fields should be private.
: 2. Declare a property for every field.
: 3. In property, initialize field value if necessary.
: That's how/when you use property.
: Some often used pattern:
: private string _connectionString = null;
: public string ConnectionString
: {

avatar
e*g
6
it's about as verbose as getter/setter. the nice thing is the concept
is in the language, not by naming convention

【在 C****n 的大作中提到】
: OK.
: The real world best pratice:
: 1. Never declare public field. All the fields should be private.
: 2. Declare a property for every field.
: 3. In property, initialize field value if necessary.
: That's how/when you use property.
: Some often used pattern:
: private string _connectionString = null;
: public string ConnectionString
: {

avatar
w*w
7
1 is a common sense, 3 is fine. but 2 ??

【在 C****n 的大作中提到】
: OK.
: The real world best pratice:
: 1. Never declare public field. All the fields should be private.
: 2. Declare a property for every field.
: 3. In property, initialize field value if necessary.
: That's how/when you use property.
: Some often used pattern:
: private string _connectionString = null;
: public string ConnectionString
: {

avatar
C*n
8
OK, I didn't say it clearly. :-)
: 2. Declare a property for every field that needs to be accessed from
outside the class. :-)

【在 w**w 的大作中提到】
: 1 is a common sense, 3 is fine. but 2 ??
avatar
c*o
9
Declare a property as PUBLIC for every ...
because private and protected can be set and got directively.

【在 C****n 的大作中提到】
: OK, I didn't say it clearly. :-)
: : 2. Declare a property for every field that needs to be accessed from
: outside the class. :-)

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。