C#List.Remove(对象)

时间:2013-10-11 21:08:44

标签: c# list xna windows-phone

我有一个List中的List对象:

public List<List<MyObject>> Grid { get; set; }

我注意到有时当我这样做时:

Grid[currentShape.ColumnNumber].Remove(currentShape);

它从数组中删除了两个对象。

我填充Grid对象的方式是,如果满足某个条件,那么我基本上就这样做了:

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;
        }
    }

0 个答案:

没有答案