您好,我在引用類型的內存分配方面有一些疑問。請澄清我在以下代碼之間註釋的問題。如何將內存分配給C#中的引用類型?
class Program
{
static void Main(string[] args)
{
testclass objtestclass1 = new testclass();
testclass objtestclass2 = new testclass();
testclass objtestclass3 = new testclass();
// Is seperate memory created for all the three objects that are created above ?
objtestclass1.setnumber(1);
objtestclass2.setnumber(2);
Console.Write(objtestclass1.number);
Console.Write(objtestclass2.number);
objtestclass3 = objtestclass1;
//When we assign one object to another object is the existing memory of the objtestclass3 be cleared by GC
Console.Write(objtestclass3.number);
objtestclass3.setnumber(3);
Console.Write(objtestclass3.number);
Console.Write(objtestclass1.number);
Console.Read();
}
public class testclass
{
public int number = 0;
public void setnumber(int a)
{
number = a;
}
}
謝謝。
嗨,喬恩,謝謝你的回答。我很自豪地說,我是你的粉絲:) – Jebli 2009-11-18 17:23:49