static ClassSubItem structItemTest() { var structItems = new List<StructItem>(); StructSubItem sub = new StructSubItem() { Str = "SubItem" }; StructSubItem sub2 = new StructSubItem() { Str = "SubItem" }; ClassSubItem cSub = new ClassSubItem() { Str = "ClassSubInStruct" }; ClassSubItem cSub2 = new ClassSubItem() { Str = "ClassSubInStruct2" }; structItems.Add(new StructItem() { Str = "Item1", Sub = sub, ClassSub = cSub}); structItems.Add(new StructItem() { Str = "Item2", Sub = sub, ClassSub = cSub }); structItems.Add(new StructItem() { Str = "Item3", Sub = sub2, ClassSub = cSub2 }); structItems.Add(new StructItem() { Str = "Item4", }); structItems.Add(new StructItem() { Str = "Item5", }); foreach (var item in structItems) { //これはコンパイルエラー //item.Str = "NewText"; } foreach (var item in structItems) { Console.WriteLine(item.ToString()); } return cSub; }
static void classItemTest(ClassSubItem cSub) { var classItems = new List<ClassItem>(); ClassSubItem sub = new ClassSubItem() { Str = "SubItem" }; ClassSubItem sub2 = new ClassSubItem() { Str = "SubItem" }; classItems.Add(new ClassItem() { Str = "Item1", Sub = sub }); classItems.Add(new ClassItem() { Str = "Item2", Sub = sub }); classItems.Add(new ClassItem() { Str = "Item3", Sub = sub2}); classItems.Add(new ClassItem() { Str = "Item4", Sub = cSub}); classItems.Add(new ClassItem() { Str = "Item5", }); foreach (var item in classItems) { Console.WriteLine(item.ToString()); } }