Skip to content

How to Resolve Error "CS0131: The left-hand side of an assignment must be a variable, property or indexer" in C#

Learn how to fix C# compiler error CS0131 by identifying invalid assignments to literals, method calls, or expressions and using proper L-values like variables or properties.

The Compiler Error CS0131 is an Assignment Syntax error. The message reads: "The left-hand side of an assignment must be a variable, property or indexer".

In programming, assignment statements follow the logic: StorageLocation = Value.

  • The Right-Hand Side (RHS) provides the value.
  • The Left-Hand Side (LHS) provides the place to store that value (often called an "L-Value").

This error occurs when the item on the left is not a valid storage location. You cannot assign a value to a number, a mathematical expression, or the return value of a method call.

Understanding L-Values

An L-Value (Left Value) is a memory address or a container that can hold data.

  • Valid L-Values: Variables (int x), Properties (obj.Name), Indexers (list[0]).
  • Invalid L-Values: Constants (5), Strings ("hello"), Math (x + y), Method returns (GetVal()).

The assignment operator = requires an L-Value on its left.

Scenario 1: Assigning to a Literal (Reverse Assignment)

This often happens due to a typo, specifically when developers try to write a comparison statement (checking if x equals 10) but accidentally use a single equals sign =, creating an assignment. If the constant is on the left (Yoda notation), CS0131 triggers.

Example of error

public void CheckValue(int x)
{
    // ⛔️ Error CS0131: The left-hand side of an assignment must be a variable...
    // You are trying to assign the value of 'x' INTO the number '10'.
    // The number 10 is immutable; it cannot change.
    if (10 = x) 
    {
        // ...
    }
}

Solution: Use Comparison Operator

If you meant to check for equality, use ==.

public void CheckValue(int x)
{
    // ✅ Correct: This is now a comparison, not an assignment.
    if (10 == x) 
    {
        Console.WriteLine("Equal!");
    }
}

Scenario 2: Assigning to a Method Call

Methods return values, but they are not variables themselves. You cannot assign a new value to a method call; you can only pass values into it.

Example of error

Trying to set a value using a "Get" method syntax.

public class User
{
    private string _name;

    public string GetName() => _name;
    public void SetName(string n) => _name = n;
}

public class Program
{
    static void Main()
    {
        var u = new User();

        // ⛔️ Error CS0131: 'GetName()' returns a string value (a copy),
        // not a storage location. You cannot assign "Alice" to it.
        u.GetName() = "Alice"; 
    }
}

Solution: Use Variables or Setters

Assign to a property or use a setter method.

public class Program
{
    static void Main()
    {
        var u = new User();

        // ✅ Correct: Use the setter method
        u.SetName("Alice");

        // Or, if using properties (Best Practice):
        // u.Name = "Alice";
    }
}

Scenario 3: Assigning to an Expression

Sometimes developers write code that resembles a mathematical equation, expecting the compiler to solve for x. C# compilers do not solve equations; they execute instructions.

Example of error

Trying to force x + 1 to equal 10.

public void MathTest()
{
    int x = 0;

    // ⛔️ Error CS0131: 'x + 1' results in a value (e.g., 1), 
    // but that result is temporary. You cannot assign '10' to it.
    x + 1 = 10;
}

Solution: Isolate the Variable

You must arrange the code so that the variable being updated is alone on the left side.

public void MathTest()
{
    int x = 0;

    // ✅ Correct: Assign the result of the math TO the variable 'x'
    x = 10 - 1;
}

Conclusion

CS0131 is the compiler telling you that your assignment arrow is pointing at a wall, not a box.

  1. Check Comparisons: Did you type = instead of ==?
  2. Check Order: Is your variable on the left and your value on the right? (x = 5, not 5 = x).
  3. Check Targets: Are you assigning to a variable/property? You cannot assign to a method call or a math expression.
Navigation