C#基本特性一
public class Product
{
private int productID;
private string name;
private string description;
private decimal price;
private string category;
public int ProductID
{
get{ return productID;}
set{ productID = value;}
}
public int name
{
get{ return productID;}
set{ productID = value;}
}
public int description
{
get{ return productID;}
set{ productID = value;}
}
}
…………
=======================================
以上属性定义不够灵活,我们希望具有灵活性,但此刻又不需要自定义的 getter 和 setter 。解决方案是一种自动实现的属性, 称之为“自动属性(Automatic Property)”
上面的代码如下:
public class Product
{
public int ProductID { get; set;}
public string name { get; set;}
public string description { get; set;}
public decimal price { get; set;}
public int category { get; set;}
}
在使用自动属性时有两个关键点:
1、不定义getter和setter的体
2、不定义该属性返回的字段
这两者都有C#编译器在这个类被建立是自动完成