3
所以我在學習C#,而且我正在寫一個程序時遇到了一些麻煩。我只是想檢查我是否理解變量賦值權。下列行爲是否像我想的那樣?C#check我很理解分配權
SomeObject someObject; // declares a SomeObject object called someObject
SomeObject someReference; // declares a SomeObject object called someReference
SomeObject someOtherObject; // declares a SomeObject object called someOtherObject
someObject = new SomeObject(); // initialises a new SomeObject object into someObject using SomeObject's contructor
someOtherObject = new SomeObject(); // initialises a new SomeObject object into someOtherObject using SomeObject's constructor
someReference = someObject; // someReference is now a reference pointing to the same place as someObject
someReference.attribute = value; // sets someReference's attribute attribute to value. someObject.attribute is also now value
someReference = someOtherObject; // someReference now points to someOtherObject instead of someObject
someReference.attribute = value2; // someOtherObject.attribute is now value2. someObject.attribute is unaffected
someReference = null; // sets someReference to be a null reference. someObject and someOtherObject are unaffected.
是的,你的理解是正確的。但是我想你在問你之前已經自己檢查過了,是不是? –
如果您「編寫程序時遇到困難」,請創建一個能夠重現問題的[mcve]。 – CodeCaster
作爲**「分配」**的含義,請注意標題**「是一個約見某人的祕密,通常是由戀人制作的。」_ – Enigmativity