我是OOP的新手,所以请考虑这一点。我将从匹配中获得的数据放入类的对象中,但是它在foreach循环中完成,因此每次调用时都会覆盖对象中的数据,最后我希望将所有数据放入我的目的。但我只有从最后一场比赛。我应该怎么做才能避免这种覆盖?也许我以完全错误的方式去做?C#类对象覆盖
foreach (var match in matches)
{
dataTable.Rows.Add(new Group[] { match.Groups["C0"], match.Groups["C1"], match.Groups["C2"], match.Groups["C3"], match.Groups["C4"] });
MyClass sk = new MyClass();
sk.Category = match.Groups["C0"].ToString();
sk.Device = match.Groups["C1"].ToString();
sk.Data_Type = match.Groups["C2"].ToString();
sk.Value = match.Groups["C3"].ToString();
sk.Status = match.Groups["C4"].ToString();
}
什么是你'sk'呢? –
在循环的第一行中,您正在向'dataTable'添加一个新行。你不会在这里覆盖任何东西。然后你创建一个MyClass对象,填充它,然后**把它扔掉**。 –
这是MyClass的对象。 – user2592968