Error: “Cannot modify the return value” c#

This is because Point is a value type (struct).

Because of this, when you access the Origin property you’re accessing a copy of the value held by the class, not the value itself as you would with a reference type (class), so if you set the X property on it then you’re setting the property on the copy and then discarding it, leaving the original value unchanged. This probably isn’t what you intended, which is why the compiler is warning you about it.

If you want to change just the X value, you need to do something like this:

Origin = new Point(10, Origin.Y);

Leave a Comment