質問

I know to perform a shallow copy in C# we could use MemberwiseClone() function but I have an object inside a function and I want to take a copy of this object, so when I added to a list it won't refer to the same object when the object is changed here is my code

public void Do(object undoState)
    {
        _index += 1;
        if (_buffer.Count > _index)
            _buffer.RemoveRange(_index, _buffer.Count - _index);
        _buffer.Add(undoState);
    }

I want to copy the UndoState object to a new object and added to the buffer

thank you

役に立ちましたか?

解決

implement the ICloneable interface, and add your copy logic into it. Now instead of receiving an object in your Do method, use ICloneable.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top