リストとクラスについて読んだ限りでは、特定のクラスのインスタンスのリストがあり、.Add() それらをリストに追加する場合、参照を追加する必要があります。したがって、元のインスタンスを変更してからリスト経由でアクセスすると、変更されたバージョンが取得されます。
これが私のコードの構造です:
public class Config()
{
public List<Item> listOfItems = new List<Item>();
public Item item1 = new Item();
public Item item2 = new Item();
...
Config()
{
listOfItems.Add(item1);
listOfItems.Add(item2);
...
}
}
public class Item
{
public string name;
Item()
{
name = "Not Set";
...
}
}
メイン コードのどこかで、Config クラスのインスタンスである currentConfig を使用します。
currentConfig.item1.name = "A";
currentConfig.item2.name = "B";
for (int i = 0; i < currentConfig.listOfItems.Count; i++)
{
DoSomething(currentConfig.listOfItems[i].name);
}
しかし、それはitem1.nameとitem2.nameの現在の値ではなく、「未設定」です。私は何を間違っていますか?
前もって感謝します!