Studies show that good grammar leads to more promotions and higher salaries. Write with Grammarly

C# Error CS0131 – The left-hand side of an assignment must be a variable, property or indexer

C# Compiler Error

CS0131 – The left-hand side of an assignment must be a variable, property or indexer

Reason for the Error

You will receive this when the left hand side of the assignment contains expression rather than a variable or property in your C# code. For example, most of the time you will receive if you have add a arithmetic operations on the left side of the assignment operator.

Let’s try to compile the below C# code snippet.

namespace DeveloperPublishNamespace
{
    public class DeveloperPublish
    {
        public static void Main()
        {
            int var1 = 10, var2 = 21, var3 = 0;
            if (var1 + var2 = var3)
                System.Console.WriteLine("Assigned the Value 3");
        }
    }
}

You will receive the error CS0131 in this case because you are using var1 + var2 on the left-hand side of the assignment.

Error CS0131 The left-hand side of an assignment must be a variable, property or indexer ConsoleApp1 C:\Users\Senthil\source\repos\ConsoleApp1\ConsoleApp1\Program.cs 8 Active

C# Error CS0131 – The left-hand side of an assignment must be a variable, property or indexer

To fix the compiler error CS0131, ensure that the left-hand side of the assignment contains only the variable, property or indexer and doesn’t include the expressions or arithmetic operators.