C#3.0自动属性,为什么不直接访问该字段? [英] C#3.0 Automatic properties, why not access the field directly?
本文介绍了C#3.0自动属性,为什么不直接访问该字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
采用将获取/设置设置在类的属性内的新方法:
With the new approach of having the get/set within the attribut of the class like that :
public string FirstName {
get; set;
}
为什么不简单地简单地将FirstName属性设置为没有访问者?
Why simply not simply put the attribute FirstName public without accessor?
推荐答案
直接访问类内部变量(字段/属性)的两个主要问题是:
Two of the big problems with direct access to variable inside class (field/attribute) are:
1)您无法轻松地对字段进行数据绑定。
1) You can't easily databind against fields.
2)如果您从类中公开了公共字段,则以后就无法将其更改为属性(例如:向验证员添加验证逻辑)
2) If you expose public fields from your classes you can't later change them to properties (for example: to add validation logic to the setters)
这篇关于C#3.0自动属性,为什么不直接访问该字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文