Q:

Rectangle.Offset method have no effect?

I have this piece of code in my super simple drawing program over here:
if (isMovingRectangle())
{
    selectedRectangle.BoundingBox.Offset(
    currentMousePos.X - mousePosWhenDown.X,
    currentMousePos.Y - mousePosWhenDown.Y);

    Invalidate();
}

I execute these statements every time when the MouseMove event occurs, but why doesn't the Offset method effect the location of the selectedRectangle???? Thank you so much for your answers.

A:

Rectangle is a value type.
Calling Offset() either returns a new Rectangle value or modifies the copy returned from the property, depending on which Rectangle you're using.
You need to assign the new Rectangle value back to the property.
In the System.Drawing.Rectangle struct, the Offset() method mutates the value, so you need to save that:
var rect = selectedRectangle.BoundingBox;
rect.Offset(...);
selectedRectangle.BoundingBox = rect;

For more information, see the evils of mutable structs.

