[問題] C#的一個List和Array的問題

作者: jamod (jasper)   2015-01-06 15:14:59
應該是有關記憶體的問題,但上網找了一些說明,還是不是很了解
=============================================
public struct s_Test{
public int i;
}
上面是我自訂的一個struct
=======================================================
s_Test t = new s_Test();
List<s_Test> list = new List<s_Test>();
list.Add(t);
list[0].i = 10;
當我用上面的方法修改list[0].i的值會出錯,
看說明是叫我new一個新的struct直接覆蓋list[0]
所以我改成:
===========================================================
List<s_Test> list = new List<s_Test>();
list.Add(t);
s_Test temp = new s_Test();
temp.i = 10;
list[0] = temp;
上面這樣才可以變更裡面的值,但是我改成陣列來儲存:
===========================================================
s_Test[] array = new s_Test[10];
array[0] = t;
array[0].i = 10;
就可以直接修改i了,想請問有沒有高手能夠解釋原因?
非常感謝!

Links booklink

Contact Us: admin [ a t ] ucptt.com