用都知道的一句话概括:“引用类型在堆上,栈上只保存引用;值类型即可存放于栈上也可存放于堆上,值类型变量直接存储值本身”。
class Program
{
static void Main(string[] args)
{
var list = new List<Person>()
{
new Person() {Name = "xfh"},
new Person() {Age = }
};
foreach (var l in list)
{
if (l.Age != ) continue;
l.Age = ;
Console.WriteLine(l.Age);
}
}
} class Person
{
public string Name { set; get; }
public int Age { set; get; }
}
若把上述代码中的Person类型由class改为struct则会出现编译错误:Foreach iteration variable 'l' is immutable.Cannot modify struct member when accessed strucy is not classified as a variable.
对于foreach
,MSDN上有句话:
The foreach statement is a wrapper around the enumerator,which only allows reading from,not writing to,the collection.
Person为class时内存分配示意图:
Person为struct时内存分配示意图:
如上图,list集合中存储的是Person实例,所以代码中的操作会使集合发生改变而引发错误。
【延申】数组的内存分配
数组元素可分为引用类型和值类型两种,其内存分配与上图中的list集合类似。
*上讨论数组中存放值类型元素时内存如何分配的几句话:
Object are always allocated on the heap.
The heap only contains objects.
Value types are either allocated on the stack, or part of an object on the heap.
An array is an object.
An array can only contain value types.
An object reference is a value type.