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。
请您参考如下方法:
Test 的每个实例都有自己的 objClassOfInts 实例。该实例将与 Test 实例一样长。
在您的示例中,这意味着它们都将一直存在到 Main 结束。
顺便说一句,最好不要使用匈牙利表示法。在“objClassOfInts”中,我们假设它是某种对象。对象在 C# 中并不特殊 - 它们是预期的。
