谁能告诉我如何动态地用数据填充 Galaxy 类?
public class Galaxy
{
public string Name {get; set;}
public string Distance {get:set;}
}
而不是手动填充它:
var theGalaxies = new List<Galaxy>
{
new Galaxy() {Name="Tadpole", Distance="200"},
new Galaxy() {Name="Andromeda", Distance="300"}
};
我想动态填充它。但是,由于 for 循环,无法编译此代码:
var theGalaxies = new List<Galaxy>
{
for (int i=0; i < someArray.Length; i++)
{
new Galaxy() {Name=someArray[0], distance=someArray[1]}
}
};
foreach (Galaxy theGalaxy in theGalaxies)
{
Console.WriteLine(theGalaxy.Name + " " + theGalaxy.Distance);
}