C# 创建类实例的列表/数组?

C# 创建类实例的列表/数组?,c#,arrays,list,class,search,C#,Arrays,List,Class,Search,我是C语言的新手,刚刚学习了如何创建自定义类。问题是,我不知道如何将这个类的40~65个实例放入一个列表/数组中,无论我需要哪个,我都可以根据其中定义的属性找到并选择一个 下面是我现在创建的类: public class Team { protected int teamNum; protected double averageMatchPoints; protected string location; protected int matchesPlayed;

我是C语言的新手,刚刚学习了如何创建自定义类。问题是,我不知道如何将这个类的40~65个实例放入一个列表/数组中,无论我需要哪个,我都可以根据其中定义的属性找到并选择一个

下面是我现在创建的类:

public class Team
{
    protected int teamNum;
    protected double averageMatchPoints;
    protected string location;
    protected int matchesPlayed;
    protected int matchesPending;
    protected int blowouts;

    //Team Number
    public void SetNumber(int num)
    {
        teamNum = num;
    }

    public int GetNumber()
    {
        return teamNum;
    }

    //Average Points per match
    public void AverageMatchPoints(double p)
    {
        averageMatchPoints = p;
    }

    public double GetAverageMatchPoints()
    {
        return averageMatchPoints;
    }

    //location information
    public void SetLocation(string l)
    {
        location = l;
    }

    public string GetLocation()
    {
        return location;
    }

    //Number of Played Matches
    public void PlayedMatches(int mat)
    {
        matchesPlayed = mat;
    }

    public int GetPlayedMatches()
    {
        return matchesPlayed;
    }

    //Number of matches pending (not played)
    public void PendingMatches(int pen)
    {
        matchesPending = pen;
    }

    public int GetPendingMatches()
    {
        return matchesPending;
    }

    //Number of Blowouts (matches where the robot was disbaled for any number of reasons)
    public void SetBlowouts(int b)
    {
        blowouts = b;
    }

    public int GetBlowouts()
    {
        return blowouts;
    }
}

现在,如果我有40~65支这样的团队参加一个活动,并且我为每个团队创建了一个此类的实例,那么我如何用每个团队编号teamNum填充一个组合框,然后根据团队编号从程序中的所有实例中找到一个特定的团队?

我推荐一个字典

// Declared somewhere
private Dictionary<int, Team> _teamDictionary = new Dictionary<int, Team>();
.
.
.
//Initialization code - I assume you have gotten your teams from a database or somewhere?
foreach (var team in myTeamsList)
{
    _teamDictionary.Add(team.teamNum, team);
}
.
.
.
// Later when you want to locate a team:
var team = _teamDictionary[selectedTeamNum];

你试过创建一个列表了吗

List<Team> Teams { get; set; }
然后可以将组合框绑定到所有团队的列表/集合/IEnumerable。要将团队初始化到40/60,请执行以下操作

for(int i = 0; i < 60; i++)
{
Team t = new Team();
t.Name = "Team 1";
t.TeamNumber = i + 1;
Teams.Add(t);
}
像这样:

将构造函数添加到您的类中,该类使用teamnumber:

如果每个团队都需要一个数字,这是最好的解决方案。因此,您不能忘记设置团队编号,因为如果不在构造函数中设置编号,就无法创建team类型的对象

填充一个组合框,并为其编号获取一个团队:

    Dictionary<int, Team> dictionary = new Dictionary<int, Team>();

    int teamNum = 1;
    // Add your Teams to a dictionary (example)
    dictionary.Add(teamNum ,new Team(teamNum++));
    dictionary.Add(teamNum, new Team(teamNum++));
    dictionary.Add(teamNum, new Team(teamNum++));

    // Populate a comboBox
    foreach(KeyValuePair<int,Team> kvp in dictionary)
    {
        comboBox1.Items.Add(kvp.Value.getTeamNum().ToString());
    }

    // get a team for a given teamNumer
    int targetTeamNumber = 2;
    if (dictionary.ContainsKey(targetTeamNumber))
    {
        Team team = dictionary[targetTeamNumber];
        // do something with the team
    }

您正在使用Windows窗体吗?另外,当您将团队添加到组合框中时,您是试图通过用户选择来定位项目,还是希望通过代码来定位项目?除了组号,组合中还会出现什么?
for(int i = 0; i < 60; i++)
{
Team t = new Team();
t.Name = "Team 1";
t.TeamNumber = i + 1;
Teams.Add(t);
}
public class Team
{
    protected int _teamNum;
    public Team(int teamNum)
    {
        _teamNum = teamNum;
    }

    public int getTeamNum()
    {
        return _teamNum;
    }

      //more logic
    }
    Dictionary<int, Team> dictionary = new Dictionary<int, Team>();

    int teamNum = 1;
    // Add your Teams to a dictionary (example)
    dictionary.Add(teamNum ,new Team(teamNum++));
    dictionary.Add(teamNum, new Team(teamNum++));
    dictionary.Add(teamNum, new Team(teamNum++));

    // Populate a comboBox
    foreach(KeyValuePair<int,Team> kvp in dictionary)
    {
        comboBox1.Items.Add(kvp.Value.getTeamNum().ToString());
    }

    // get a team for a given teamNumer
    int targetTeamNumber = 2;
    if (dictionary.ContainsKey(targetTeamNumber))
    {
        Team team = dictionary[targetTeamNumber];
        // do something with the team
    }