可能重复:
在List <T>中交换两个项目
编辑:也许这可以用于获取'b'值?
for (int i = 0; i < inventory.Count; i++)
{
if (inventory[a].ItemRectangle.Intersects(inventory[i].ItemRectangle))
{
itemB = inventory[i];
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:这是我的进步.
Item itemA;
Item itemB;
int a = -1;
int b = -1;
if (a != -1 && b != -1)
{
itemA = inventory[a];
itemB = inventory[b];
Swap(ref itemA, ref itemB);
inventory[a] = itemA;
inventory[b] = itemB;
}
Run Code Online (Sandbox Code Playgroud)
而这里是我获得'a'价值的地方.
if (item.ItemSelected == true)
{
a = item.ItemIndex;
}
else
a = -1;
Run Code Online (Sandbox Code Playgroud)
我还没弄明白如何获得'b'值,因为我必须检查一个项目是否与同一列表中的另一个项目发生冲突.如果有人知道我怎么做,请告诉我.我想这看起来像这样:
if (item.ItemRectangle.Intersects(//the other item.ItemRectangle) …Run Code Online (Sandbox Code Playgroud)