定义在一个类中,便捷写法prop+两次tab 或propfull+两次tab.
class Student2
{
private int age;
public int Age
{
get
{
return this.age;
}
set
{
if (value >= 0 && value <= 120)
{
this.age = value;
}
else
{
throw new Exception("Age value has error");
}
}
}
区别于C++和java写法
//c++ 和java没有属性的概念,通过get 和 set 方法来保护字段
class Student1
{
private int age;
public int GetAge()
{
return this.age;
}
public void SetAge(int value)
{
if (value >= 0 && value <= 120)
{
this.age = value;
}
else
{
throw new Exception("Age value has error");
}
}
}
调用
Student2 stu = new Student2();
stu.Age = 20;