2015-04-01 30 views
1

從下面的結果中,我想知道爲什麼'測試2'的值爲30, 我希望得到與'測試3'(100)相同的結果?爲什麼集合賦值在方法和連續行內的行爲不同?

這裏是提琴手鍊接爲更好的理解。

IList<Test> initialCollection = new List<Test>(); 
initialCollection.Add(new Test(){ Value = 30}); 

Console.WriteLine("Test 1 : Before update method : " + initialCollection.Last().Value); 

UpdateValueCollection(initialCollection); 

Console.WriteLine("Test 2: After update method : " + initialCollection.Last().Value); 
IList<Test> check = new List<Test>(); 
check.Add(new Test(){ Value = 100}); 
initialCollection = check; 

Console.WriteLine("Test 3: Same update method code added as consecutive line : " + initialCollection.Last().Value); 

和我的另一種方法是

public void UpdateValueCollection(IList<Test> lstTest) 
{ 
    IList<Test> check = new List<Test>(); 
    check.Add(new Test(){ Value = 100}); 
    lstTest = check; 
} 

的結果是

Test 1 : Before update method : 30 
Test 2 : After update method : 30 
Test 3 : Same update method code added as consecutive line : 100 
+1

因爲C#是通過拷貝通(除非使用'ref'或'out')。當你傳遞引用類型,即參考的副本。您可以改變它所引用的內容,但分配一個新值將不會被調用者看到。 – 2015-04-01 17:25:35

回答

3

之前的第二次測試你傳遞initialCollection值的副本UpdateValueCollection,在該方法中,你無視傳遞的值,創建一個新列表,然後修改該列表。這個方法永遠不會對這個方法的任何調用者有任何可觀察的影響。

測試3之前創建一個新的列表,給它一個值,然後將其分配給initialCollection變異的變量值。由於您已更改此變量的值,與第二次測試不同,因此當您稍後獲取該變量的值時,它會引起可觀察的效果。

由值假如UpdateValueCollection通過引用(通過使用refout關鍵字的)傳遞的參數的方法,而不是,則改變該參數將也影響initialCollection,但是因爲它是通過按值,只有變量的副本發生了變異。

請注意,如果你真的想UpdateValueCollection來計算您的變量一個新的列表更慣用的設計將有UpdateValueCollection返回而不是void一個新的列表,然後給該方法的值賦給initialCollection

1

當你調用內UpdateValueCollectionlstTest = check;你只更新基準lstTest該方法中可見,則需要按引用傳遞lstTest能夠更換名單本身具有的UpdateValueCollection方法外部可見:

public void UpdateValueCollection(ref IList<Test> lstTest) 
{ 
    IList<Test> check = new List<Test>(); 
    check.Add(new Test(){ Value = 100}); 
    lstTest = check; 
} 
0

這些「奇怪」的事情可能發生,因爲你給你的方法提到一個IList。如果您現在更改您的IList的值,則只會使用此方法更新參考。 這意味着你現在在你的RAM的其他部分進行遊戲。

要解決此問題,您必須更改對IList參考的引用。 喜歡這張

public void UpdateValueCollection(ref IList<Test> lstTest) 
{ 
    IList<Test> check = new List<Test>(); 
    check.Add(new Test(){ Value = 100}); 
    lstTest = check; 
} 
相關問題