我正在尝试理解C#中集合的用法,并且还尝试了如何基于msdn示例中给出的语句定义类,因为它的类未发布并留给读者。我提出了以下课程,但需要知道这是对还是错:
声明说:
// ListLeagueList is a list of League objects.
// Each League object has a Name and a collection of Division objects.
// Each Division has a Name and a collection of Team objects,
// and each Team object has a Name.
public class Team
{
private string teamName;
public string TeamName
{
get { return teamName; }
set { teamName = value; }
}
}
public class Division
{
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
public List<Team> DivisionList;
}
public class League
{
private string Name;
public string name
{
get { return name; }
set { name = value; }
}
List<Division> DivisionList;
}
感谢。
答案 0 :(得分:2)
看似正确,但您已将List<Team>
命名为DivisionList
..我会将其更改为TeamList
:
public class Division
{
public string Name { get; set; }
public List<Team> TeamList { get; set; } // Change this, because its a List of Teams.
}
编辑:
吉利打败了我的速记属性:$答案 1 :(得分:1)
您可以使用Auto-Implemented Properties:
来缩短属性定义public string Name { get; set; }
这样,您不需要支持字段。如果稍后由于某种原因需要使用支持字段,请在此时添加。