我有這個代碼:這2個片段嚴格等效嗎?
foreach (object obj in _Destinations)
{
obj = _ItemsSource;
}
這是產生錯誤
不能分配給 'OBJ',因爲它是一個 '的foreach迭代變量'
所以我「盤旋「與以下代碼的問題:
for (int i = 0; i < _Destinations.Count; i++)
{
_Destinations[i] = _ItemsSource;
}
這適用於我的實際案例,但我不確定這是否與任何類型的對象完全相同。是這樣嗎?
編輯:_Destinations是List<object>
這個線程可能會讓人感興趣,爲什麼'foreach'循環爲'for'循環表現出不同的行爲:http://stackoverflow.com/questions/5650705/in-c-why-cant-i-modify-the-成員的價值型實例在一個foreach循環 – keyboardP 2011-12-22 13:12:29