当我将元素添加到字典中时,这些元素也会添加到另一个字典中(C#+;XNA)
我有一些代码如下所示:当我将元素添加到字典中时,这些元素也会添加到另一个字典中(C#+;XNA),c#,dictionary,xna,C#,Dictionary,Xna,我有一些代码如下所示: public static class Control { public static Dictionary<PlayerIndex, GamePadState> gamePadState = new Dictionary<PlayerIndex,GamePadState>(); public static Dictionary<PlayerIndex, GamePadState> oldGamePadState = n
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=新字典();
公共静态字典oldGamePadState=新字典();
公共静态void updateControls()
{
gamePadState.Clear();
foreach(pIndex数组中的PlayerIndex pIndex)
{gamePadState.Add(pIndex,GamePad.GetState(pIndex));}
}
}
当我在调试中查看代码时,当我调用gamePadState.Add(…);,它还添加了oldGamePadState,尽管我从未调用过oldGamePadState.Add(…) 很有可能在其他地方有代码将项添加到词典中。我看到它们都是公开的。也许最好将它们设置为私有的,并且只通过包装器方法公开字典方法。然后,您可以在这些包装器方法中设置断点,以找出访问词典的其他代码 例如:
public static class Control
{
//Change these to private
private static Dictionary<PlayerIndex, GamePadState> gamePadState = new Dictionary<PlayerIndex,GamePadState>();
private static Dictionary<PlayerIndex, GamePadState> oldGamePadState = new Dictionary<PlayerIndex, GamePadState>();
public void AddOld(PlayerIndex index, GamePadState state)
{
oldGamePadState[index] = state; // Set breakpoint here
// When the breakpoint trips, look at the stack trace to find out
// who is calling this method
}
public void AddNew(PlayerIndex index, GamePadState state)
{
gamePadState[index] = state;
}
}
公共静态类控件
{
//把这些换成私人的
私有静态字典gamePadState=新字典();
私有静态字典oldGamePadState=新字典();
public void AddOld(PlayerIndex索引,GamePadState)
{
oldGamePadState[index]=state;//在此处设置断点
//当断点跳闸时,查看堆栈跟踪以了解
//谁在调用此方法
}
public void AddNew(PlayerIndex索引,GamePadState)
{
gamePadState[索引]=状态;
}
}
有关为什么使用公共属性(getter和setter)而不是普通的旧公共变量通常是一个好主意的详细信息,请参阅。很可能在其他地方有代码将项添加到词典中。我看到它们都是公开的。也许最好将它们设置为私有的,并且只通过包装器方法公开字典方法。然后,您可以在这些包装器方法中设置断点,以找出访问词典的其他代码 例如:
public static class Control
{
//Change these to private
private static Dictionary<PlayerIndex, GamePadState> gamePadState = new Dictionary<PlayerIndex,GamePadState>();
private static Dictionary<PlayerIndex, GamePadState> oldGamePadState = new Dictionary<PlayerIndex, GamePadState>();
public void AddOld(PlayerIndex index, GamePadState state)
{
oldGamePadState[index] = state; // Set breakpoint here
// When the breakpoint trips, look at the stack trace to find out
// who is calling this method
}
public void AddNew(PlayerIndex index, GamePadState state)
{
gamePadState[index] = state;
}
}
公共静态类控件
{
//把这些换成私人的
私有静态字典gamePadState=新字典();
私有静态字典oldGamePadState=新字典();
public void AddOld(PlayerIndex索引,GamePadState)
{
oldGamePadState[index]=state;//在此处设置断点
//当断点跳闸时,查看堆栈跟踪以了解
//谁在调用此方法
}
public void AddNew(PlayerIndex索引,GamePadState)
{
gamePadState[索引]=状态;
}
}
有关为什么使用公共属性(getter和setter)而不是普通的旧公共变量通常是一个好主意的详细信息,请参阅。我怀疑您实际上只有一个字典,并且您在某个地方有一些代码正在执行
Control.oldGamePadState = Control.gamePadState;
(反之亦然)
它不会将dictionary对象从一个变量复制到另一个变量,而是复制引用,因此在该语句之后,它们都将引用同一个dictionary。如果这对你来说是个惊喜,读我的
正如Phil所说,你应该考虑把它们变成私有的,我也建议你让变量只读。这不会使字典成为只读的-它只会阻止变量被重新分配。我怀疑您实际上只有一个字典,并且您在某个地方有一些代码正在执行
Control.oldGamePadState = Control.gamePadState;
(反之亦然)
它不会将dictionary对象从一个变量复制到另一个变量,而是复制引用,因此在该语句之后,它们都将引用同一个dictionary。如果这对你来说是个惊喜,读我的
正如Phil所说,你应该考虑把它们变成私有的,我也建议你让变量只读。这不会使字典成为只读的-它只会阻止变量被重新分配。若要验证是否是这种情况,请在
.Add()
行上放置一个断点,然后在调试器中计算Object.ReferenceEquals(),以查看两个字典引用是否相同。若要验证是否是这种情况,在.Add()
行上放置断点,并在调试器中计算Object.ReferenceEquals(),以查看两个字典引用是否相同。