我已经尝试谷歌搜索两天,但似乎无法找到答案。
我希望 Category 类根据输入的 id 提供描述,如果 id 无效则返回错误。这是最好的方法吗?
public class Category
{
private int _id;
private string _desc;
public Category(int id)
{
ID = id;
}
public int ID
{
get
{
return _id;
}
set
{
_id = value;
//_desc = value from data access layer or throw error if the ID is invalid
}
}
public string Description
{
get
{
return _desc;
}
}
}
public class Person
{
public int ID {get; set;}
public Category Category {get; set;}
}
public class MyApp
{
static void Main()
{
Person p = new Person();
Category c = new Category(2);
p.Category = c;
}
}