我有一个列表,类似这样的“ List <Block>”。我想要的是获得一个与列表中的块相同的块,而不是列表中的对象。并用块修改圆顶并获得另一个。
但是问题是,在获取了三次块(列表的长度为3)之后,第四个块已经被修改。我尝试了我所知道的所有方法,甚至使用“ new”,它只是将对象添加到列表中,而不是同一对象。
那么如何解决呢?
这是我的一些代码:
//This is the list which length is 3
private List<BlockType> blocks;
//At the beginning it was like this but not work
//private List<Block> blocks;
//In a function to get a block type randomly
int blockNum = rand.Next(0, 3); //rand is a Random type
this.cBlock = new Block(blocks[blockNum]); //cBlock is object which I use to do something about the block
//The class Block goes to
class Block
{
private List<Rectangle> _block;
public List<Rectangle> block
{
get { return _block; }
}
private int _blockNum;
public int blockNum
{
get { return _blockNum; }
}
public Block()
{
}
public Block(int blockNum, List<Rectangle> block)
{
this._block = block;
this._blockNum = blockNum;
}
public Block(BlockType block)
{
this._block = block.block;
this._blockNum = block.blockNum;
}
}
//And the BlockType is what I tried but does not work
class BlockType
{
private List<Rectangle> _block;
public List<Rectangle> block
{
get { return _block; }
}
private int _blockNum;
public int blockNum
{
get { return _blockNum; }
}
public BlockType()
{
}
public BlockType(int blockNum, List<Rectangle> block)
{
this._block = block;
this._blockNum = blockNum;
}
}
桃花长相依
相关分类