我有一些代碼,看起來像這樣:當我元素添加到詞典時,該元素也添加到另一個字典(C#+ XNA)
public static class Control
{
public static Dictionary<PlayerIndex, GamePadState> gamePadState = new Dictionary<PlayerIndex,GamePadState>();
public static Dictionary<PlayerIndex, GamePadState> oldGamePadState = new Dictionary<PlayerIndex, GamePadState>();
public static void UpdateControlls()
{
gamePadState.Clear();
foreach (PlayerIndex pIndex in pIndexArray)
{ gamePadState.Add(pIndex,GamePad.GetState(pIndex)); }
}
}
正如我通過在調試,當代碼看上去我稱gamePadState.Add(...);它也添加到oldGamePadState,即使我從來沒有調用oldGamePadState.Add(...);
要驗證是否出現這種情況,請在`.Add()`行上放置一個斷點並評估Object.ReferenceEquals()(http://msdn.microsoft.com/zh-cn/library/system.object .referenceequals.aspx)在調試器中查看兩個字典引用是否相同。 – Bevan 2011-01-29 08:00:36