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 离好几百行的时候, 够费眼的.)
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 离好几百行的时候, 够费眼的.)
L*r
4 楼
还有一点,如果你用VS.Net可以很方便的显示和管理他们。
【在 p***n 的大作中提到】
: 可以用来实现比较严密的封装,控制外界与类的FIELD的接触.
: 实际上,不管是在C++还是在JAVA(e.g.,Bean)里头,用getter/setter 函数
: (accessor,mutator)来控制外界读写的方式一经很常用了,C#只不过是在语
: 言里直接把这个常用的模式正式吸纳而已.
: 譬如,如果你想让一个属性只读, 就只定义一个GET好了. 很方便. 而且如果
: 你想改变属性的名称, 只用在一个地方改就行了.(呵呵, 如果在JAVA里摊上
: 个getter/setter 离好几百行的时候, 够费眼的.)
【在 p***n 的大作中提到】
: 可以用来实现比较严密的封装,控制外界与类的FIELD的接触.
: 实际上,不管是在C++还是在JAVA(e.g.,Bean)里头,用getter/setter 函数
: (accessor,mutator)来控制外界读写的方式一经很常用了,C#只不过是在语
: 言里直接把这个常用的模式正式吸纳而已.
: 譬如,如果你想让一个属性只读, 就只定义一个GET好了. 很方便. 而且如果
: 你想改变属性的名称, 只用在一个地方改就行了.(呵呵, 如果在JAVA里摊上
: 个getter/setter 离好几百行的时候, 够费眼的.)
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
: {
【在 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
: {
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
: {
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
: {
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
: {
【在 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
: {
相关阅读
转载 borland的eco真的很酷分特,才看到开版。。.NET 入门系列(1)--什么是.NETopen source dotNet IDERSS 简介 Re: HttpWebRequest小程序using weather XML feed in C#csunitC# on JVM? 我的.NET 书架screebshot: winform based weather app.Net search engine问一个吵架笨笨,菩萨怕怕stupid C#问题open source dotNet IDE.NET 入门系列(2)--Web Services,XML,SOAP Free ASP.NET Training[FWD]C#4JavaErs我的第一个小C#程序away呢? Created a group on YahooNdoc