Coding⏱️ 3 min read📅 2026-06-03

How to Fix: Error: "Cannot modify the return value" in C#

Error in auto-implemented properties. Declare own backing variable.

Quick Answer: Declare a private field to store the backing value, e.g., private Point _origin; public Point Origin { get; set; } = new Point(0, 0);

The 'Cannot modify the return value' error in C# occurs when attempting to assign a value to a read-only property. This issue affects developers who use auto-implemented properties without declaring their own backing variables.

This error is frustrating because it prevents developers from making changes to the values of these properties. However, there are several methods to resolve this issue.

🛑 Root Causes of the Error

  • The primary reason for this error is that auto-implemented properties in C# do not have a backing variable by default.
  • This can be resolved by declaring your own backing variable within the property declaration.

🛠️ Step-by-Step Verified Fixes

Declaring a backing variable

  1. Step 1: To declare a backing variable, use the private keyword before the type of the property.
  2. Step 2: For example: private int _originX; and then public Point Origin { get; set; } = new Point(_originX, _originY);
  3. Step 3: This way, you can modify the value of the property without getting the error.

Using an expression body

  1. Step 1: Another method to resolve this issue is by using an expression body for the property.
  2. Step 2: For example: public Point Origin => new Point(10, 20); This way, you can directly assign a value to the property without declaring a backing variable.

✨ Wrapping Up

By declaring your own backing variable or using an expression body, developers can resolve the 'Cannot modify the return value' error in C# and make changes to their auto-implemented properties.

Did this fix your problem?

If not, try searching for specific error codes.

🔍 Search Error Database

❓ Frequently Asked Questions