Question

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

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top