我想弄清楚为什么分配器(myC.myself)在编译行后变为null? 提前谢谢。
namespace AequalsBequalsC
{
class Program
{
static void Main(string[] args)
{
A myA = new A();
A myB = new A();
myB.a = 8;
A myC = new A();
myC.a = 9;
myC.myself = myA;
myC = myC.myself;
Console.WriteLine("myA.a is {0}, myB.a is {1}, myC.a is {2}", myA.a,myB.a, myC.a);
Console.ReadLine();
}
}
public class A
{
public int a = 4;
public A myself;
}
}
答案 0 :(得分:2)
永远不会成为null
。 始终为空。当您创建A类的新实例时,我自己variable
永远不会被分配,因此是null
。
当您将myC.myself
myA
值分配给myA
,然后将myC
值分配给myC
时,myself
不再是您创建的第三个对象,但第一个,从来没有 class Program
{
static void Main(string[] args)
{
A myA = new A();
A myB = new A();
myB.a = 8;
A myC = new A();
myC.a = 9;
myC.myself = myA;
myC = myC.myself;
Console.WriteLine("myA.a is {0}, myB.a is {1}, myC.a is {2}", myA.a, myB.a, myC.a);
Console.ReadLine();
}
}
public class A
{
public int a = 4;
public A myself;
public A()
{
this.myself = this;
}
}
设置在第一位。
现在,如果您做了类似的事情(我强烈建议不要这样做),那么它就不会为空:
/*You can replace theTextFieldYouAreTalkingAbout with the one you are talking about*/
theTextFieldYouAreTalkingAbout.text = ""