0

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);
}
4

5 に答える 5

0

それはC#のように見えます。

var theGalaxies = new List<Galaxy>():
Galaxy galxy;
for (int i = 0; i < someArray.Length; i++)
{
    galxy =new Galaxy() {Name = someArray[0], distance = someArray[1]};
    theGalaxies.Add(galxy);
};
于 2013-08-30T21:19:27.630 に答える