【发布时间】:2011-01-05 12:10:51
【问题描述】:
using System;
class ClassOfInts
{
public int x;
public int y;
}
class Test
{
ClassOfInts objClassOfInts;
string name;
public TestMethod(int p, int q, string s)
{
objClassOfInts=new ClassofInts;
objClassOfInts.x=p;
objClassOfInts.y=q;
name=s;
}
}
class Main
{
static Main()
{
Test t1=new Test();
Test t2=new Test();
t1.TestMethod(1,2,"First");
//XXX
t2.TestMethod(2,3,"Second");
//YYY
}
}
上述程序到达XXX时的内存分配是多少。引用变量 objClassInts 是否仍会引用其在堆中的对象。或 TestMethod 完成执行后,objClassInts 将引用为 null。
【问题讨论】:
-
我认为当你到达 XXX 时
objClassInts不会为空是很安全的,因为你仍然有对t1的引用,而objClassInts是类@987654325 的类范围变量@ 刚刚通过调用TestMethod设置。
标签: c# .net oop memory-management