关于引用/集合/值类型的另一个C#问题
我有以下代码:关于引用/集合/值类型的另一个C#问题,c#,.net,stack,heap,C#,.net,Stack,Heap,我有以下代码: public class Test { public static void Main() { List<Person> list = new List<Person>(); Person person = new Person() { Name="Chris" }; list.Add(person); person = new Person(){ Name="Wilson
public class Test
{
public static void Main()
{
List<Person> list = new List<Person>();
Person person = new Person() { Name="Chris" };
list.Add(person);
person = new Person(){ Name="Wilson the cat" };
list.Add(person);
Console.WriteLine(list[0].Name);
Console.WriteLine(list[1].Name);
Console.ReadLine();
}
}
public class Person
{
public string Name {get;set;}
}
公共类测试
{
公共静态void Main()
{
列表=新列表();
Person Person=newperson(){Name=“Chris”};
列表。添加(人);
person=newperson(){Name=“Wilson the cat”};
列表。添加(人);
Console.WriteLine(列表[0]。名称);
Console.WriteLine(列表[1]。名称);
Console.ReadLine();
}
}
公共阶层人士
{
公共字符串名称{get;set;}
}
我的问题是,第一人称实例去了哪里?CLR是否神奇地在某个地方创建了它的新实例?在列表之外是否有引用它的方法?例如,在方法完成后,它会去哪里?在集合中存储对象使用什么方法(这是4个问题)。1)它放在堆上(如果>84K,则放在大对象堆上)
2) 是,将创建一个新实例。可以通过列表访问它来引用它
3) 该集合可能使用列表,但除非您需要特定的速度或空间属性,否则不需要了解内部详细信息。list list list=new list();
List<Person> list = new List<Person>();
Person person = new Person() { Name="Chris" };
// your person object lives on the heap. The person variable is just
// a pointer of that object on the heap. Note that the pointer lives
// on the stack, and the object it points to lives on the heap.
list.Add(person);
// when you add your person to to the list, all it does it pass a
// copy of the *pointer* to the list's method. List has access to this
// object through the pointer.
person = new Person(){ Name="Wilson the cat" };
// new'ing up a instance of person creates another person object on
// the heap. Note that this doesn't overwrite our original person created above,
// because our original person sits in an entirely differently memory
// location.
// We did, however overwrite our pointer variable by assigning it a new
// location to point at. This doesn't affect the object we put into our
// list since the list received a copy of our original pointer :)
list.Add(person);
Console.WriteLine(list[0].Name);
// list[0] has a pointer to the first object we created
Console.WriteLine(list[1].Name);
// list[1] has a pointer to the second object we created.
Console.ReadLine();
// when this methods goes out of scope (i.e. when the stack frame is
// popped), the pointers will be dropped from memory, and the objects
// on the heap will no longer have any live references to them, so
// they'll be eaten by the garbage collector.
Person Person=newperson(){Name=“Chris”};
//您的person对象位于堆上。person变量只是
//堆上该对象的指针。请注意,指针仍然有效
//在堆栈上,它指向的对象位于堆上。
列表。添加(人);
//当你将你的人添加到列表中时,它所做的一切都会通过
//指向列表方法的*指针*的副本。列表可以访问此文件
//对象通过指针。
person=newperson(){Name=“Wilson the cat”};
//新建person实例会在上创建另一个person对象
//堆。请注意,这不会覆盖上面创建的原始人员,
//因为我们最初的人坐在一个完全不同的记忆里
//地点。
//但是,我们确实通过为指针变量指定一个新的
//指向的位置。这不会影响我们放入我们的文件中的对象
//列表,因为列表收到了原始指针的副本:)
列表。添加(人);
Console.WriteLine(列表[0]。名称);
//列表[0]有一个指向我们创建的第一个对象的指针
Console.WriteLine(列表[1]。名称);
//列表[1]有一个指向我们创建的第二个对象的指针。
Console.ReadLine();
//当此方法超出范围时(即当堆栈帧
//指针将从内存中删除,对象
//堆上不再有任何活动引用,因此
//它们会被垃圾收集器吃掉。
和个人。。。在方法完成之前是否在堆栈上?我的问题更多的是关于泛型列表,而不是旧的堆栈/堆列表