几个.net疑问用法讨论
1.属性用法
现象:看过部分人项目中很喜欢用属性来“替换变量”。很惭愧到现在我虽然知道属性怎么声明、使用。但一直还是体会不出使用属性用法有何好处?
好处个人感觉有几个.A.可设置变量可修改还是不可修改; B.可设置内部私有变量通过外部进行传值或取值(要求该属性设置访问级别可访问可修改)
大家可看看底下几种用法到底有没有必要,如果这样做有什么好处?
//属性用法一public class CustomnerType{ private string strCustomerType; public string CustomerTypeName { get { return strCustomerType; } set { strCustomerType= value; }} public partial class _Default : System.Web.UI.Page{ protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { CustomnerType objCustomner= new CustomnerType(); txtCustomnerType.Text = objCustomner.CustomerTypeName; } } protected void Button1_Click(object sender, EventArgs e) { CustomnerType objCustomner= new CustomnerType(); string CustomerTypeName = txtCustomerTypeName; objCustomner.CustomerTypeName = CustomerTypeName; }}//大家说看看这样用法有必要吗?有必要的话,有什么好处//属性讨论二,也是让我疑问最大的用法public class SqlAction{ private DataSet objSet; } public DataTable GetTable // 提供一个可供利用的数据源 { get { return objSet.Tables[0]; } }如需要阅读该回复,请登录或注册CSDN!