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.

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

This C# program calculates and displays an upper triangular matrix based on user input. Problem Statement: The program takes the...
This C# program serves as a demonstration of bitwise operators, which are fundamental operators used for manipulating individual bits in...
This C# program is designed to interchange or swap the columns of a matrix. A matrix is a two-dimensional array...