6

我希望我的术语在这里是正确的,仍然学习所有正确的术语。

我使用以下代码创建了一个自定义类:

    public class inputData
{
    public string type;
    public int time;
    public int score;
    public int height;
}

我想创建一个包含该类的列表,就像我在这里所做的那样:

List<inputData> inputList = new List<inputData>();

我现在正在尝试添加到该列表中,但遇到了麻烦。我已经尝试了以下两种方法,但仍然没有运气。有人可以在这里指出我正确的方向吗?

inputList.Add(new inputData("1", 2, 3, 4));

inputList.type.Add("1"); 
4

2 回答 2

12

你需要对象初始化器

改变

inputList.Add(new inputData("1", 2, 3, 4));

inputList.Add(new inputData{type="1", time=2, score=3, height=4});
于 2012-11-12T06:39:03.980 回答
2

问题不在于列表,而在于 inputData 类 - 您正在尝试使用未定义的构造函数。将构造函数添加到 inputData 类中:

public inputData(string type, int time, int score, int height)
{
  this.type=type; this.time=time, this.score=score, this.height=height
}

其次,遵循 C# 约定 - 类名应以大写开头,公共字段替换为 C# 属性。但这不是您的代码不起作用的问题。

于 2012-11-12T06:44:54.513 回答