C#List.Remove(对象)

C#List.Remove(对象),c#,list,xna,windows-phone,C#,List,Xna,Windows Phone,我有一个对象,它是列表中的列表: public List<List<MyObject>> Grid { get; set; } 它从数组中删除两个对象 我填充网格对象的方式是,如果满足某个条件,那么我基本上会: Grid[newShape.ColumnNumber].Add(newShape); currentShape = newShape; currentShape是一个类变量,我使用它在屏幕上移动对象。是否因为引用类型,当我有时执行.Remove()时,它会从数

我有一个对象,它是
列表中的
列表

public List<List<MyObject>> Grid { get; set; }
它从数组中删除两个对象

我填充
网格
对象的方式是,如果满足某个条件,那么我基本上会:

Grid[newShape.ColumnNumber].Add(newShape);
currentShape = newShape;
currentShape
是一个类变量,我使用它在屏幕上移动对象。是否因为引用类型,当我有时执行
.Remove()
时,它会从数组中删除多个对象?我是否需要以某种方式扩展
.Remove()

非常感谢您的帮助

更新-整个方法:

    public void MoveIfPossible(MoveDirection direction)
    {
        List<Shape> neighbouringShapes = new List<Shape>();

        switch (direction)
        {
            case MoveDirection.Right:
                if (currentShape.ColumnNumber == utilities.columns.Count - 1)
                    return;

                neighbouringShapes = GetNeighbouringShapes(currentShape.ColumnNumber);

                foreach (var s in neighbouringShapes)
                {
                    if (currentShape.Position.X + 1 < s.Position.X && currentShape.Position.Y + currentShape.Texture.Height >= s.Position.Y)
                    {
                        return;
                    }
                }
                world.Grid[currentShape.ColumnNumber].Remove(currentShape);
                world.Grid[currentShape.ColumnNumber + 1].Add(currentShape);

                currentShape.Position.X = utilities.columns[currentShape.ColumnNumber + 1].X;
                currentShape.ColumnNumber++;

                currentShape.Coordinate.X = currentShape.ColumnNumber;
                currentShape.Coordinate.Y = world.Grid[currentShape.ColumnNumber].Count - 1;
                break;

            case MoveDirection.Left:
                if (currentShape.ColumnNumber == 0)
                    return;

                neighbouringShapes = GetNeighbouringShapes(currentShape.ColumnNumber - 1);

                foreach (var s in neighbouringShapes)
                {
                    if (currentShape.Position.X - 1 > s.Position.X && currentShape.Position.Y + currentShape.Texture.Height >= s.Position.Y)
                    {
                        return;
                    }
                }
                world.Grid[currentShape.ColumnNumber].Remove(currentShape);
                world.Grid[currentShape.ColumnNumber - 1].Add(currentShape);

                currentShape.Position.X = utilities.columns[currentShape.ColumnNumber - 1].X;
                currentShape.ColumnNumber--;

                currentShape.Coordinate.X = currentShape.ColumnNumber;
                currentShape.Coordinate.Y = world.Grid[currentShape.ColumnNumber].Count;
                break;
        }
    }
public void moveif可能(MoveDirection)
{
List neighbouringShapes=新建列表();
开关(方向)
{
案例移动方向。右:
if(currentShape.ColumnNumber==utilities.columns.Count-1)
返回;
neighbouringShapes=GetNeighbouringShapes(currentShape.ColumnNumber);
foreach(相邻形状中的变量s)
{
如果(currentShape.Position.X+1=s.Position.Y)
{
返回;
}
}
网格[currentShape.ColumnNumber]。删除(currentShape);
网格[currentShape.ColumnNumber+1]。添加(currentShape);
currentShape.Position.X=实用程序.columns[currentShape.ColumnNumber+1].X;
currentShape.ColumnNumber++;
currentShape.Coordinate.X=currentShape.ColumnNumber;
currentShape.Coordinate.Y=world.Grid[currentShape.ColumnNumber].Count-1;
打破
案例移动方向。左:
如果(currentShape.ColumnNumber==0)
返回;
neighbouringShapes=GetNeighbouringShapes(currentShape.ColumnNumber-1);
foreach(相邻形状中的变量s)
{
如果(currentShape.Position.X-1>s.Position.X&¤tShape.Position.Y+currentShape.Texture.Height>=s.Position.Y)
{
返回;
}
}
网格[currentShape.ColumnNumber]。删除(currentShape);
网格[currentShape.ColumnNumber-1].Add(currentShape);
currentShape.Position.X=实用程序.columns[currentShape.ColumnNumber-1].X;
currentShape.ColumnNumber--;
currentShape.Coordinate.X=currentShape.ColumnNumber;
currentShape.Coordinate.Y=world.Grid[currentShape.ColumnNumber].Count;
打破
}
}

文档中说明仅删除第一个。很可能您正在调用Remove两次。我没有调用.Remove()两次。“我只给它打过一次电话。”苏比——对此要非常肯定。在那里放一个Debug.WriteLine()。这看起来可能是一个多次触发的事件。整个类只有一次出现
.Remove()
,这在上面的代码中。因此,我不可能调用它两次。他是在询问您的代码片段是否在事件处理程序中-仅仅因为您键入了一次
Remove
,并不意味着包含的方法没有被调用多次。
    public void MoveIfPossible(MoveDirection direction)
    {
        List<Shape> neighbouringShapes = new List<Shape>();

        switch (direction)
        {
            case MoveDirection.Right:
                if (currentShape.ColumnNumber == utilities.columns.Count - 1)
                    return;

                neighbouringShapes = GetNeighbouringShapes(currentShape.ColumnNumber);

                foreach (var s in neighbouringShapes)
                {
                    if (currentShape.Position.X + 1 < s.Position.X && currentShape.Position.Y + currentShape.Texture.Height >= s.Position.Y)
                    {
                        return;
                    }
                }
                world.Grid[currentShape.ColumnNumber].Remove(currentShape);
                world.Grid[currentShape.ColumnNumber + 1].Add(currentShape);

                currentShape.Position.X = utilities.columns[currentShape.ColumnNumber + 1].X;
                currentShape.ColumnNumber++;

                currentShape.Coordinate.X = currentShape.ColumnNumber;
                currentShape.Coordinate.Y = world.Grid[currentShape.ColumnNumber].Count - 1;
                break;

            case MoveDirection.Left:
                if (currentShape.ColumnNumber == 0)
                    return;

                neighbouringShapes = GetNeighbouringShapes(currentShape.ColumnNumber - 1);

                foreach (var s in neighbouringShapes)
                {
                    if (currentShape.Position.X - 1 > s.Position.X && currentShape.Position.Y + currentShape.Texture.Height >= s.Position.Y)
                    {
                        return;
                    }
                }
                world.Grid[currentShape.ColumnNumber].Remove(currentShape);
                world.Grid[currentShape.ColumnNumber - 1].Add(currentShape);

                currentShape.Position.X = utilities.columns[currentShape.ColumnNumber - 1].X;
                currentShape.ColumnNumber--;

                currentShape.Coordinate.X = currentShape.ColumnNumber;
                currentShape.Coordinate.Y = world.Grid[currentShape.ColumnNumber].Count;
                break;
        }
    }