[问题] 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