Skip to main content

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

· One min read
Jagdish Kumawat
Founder @ Dewiride

(2,1): error CS0131: The left-hand side of an assignment must be a variable, property or indexer. It's important to notice that assignment happens from right to left.

The following code led to the error -

string firstName;
"Bob" = firstName;

Detailed error message -

(2,1): error CS0131: The left-hand side of an assignment must be a variable, property or indexer
(2,9): error CS0165: Use of unassigned local variable 'firstName'

Issue

Improperly assigned a value to a variable

It's important to notice that assignment happens from right to left. In other words, the C# compiler must first understand the value on the right side of the assignment operator, then it can perform the assignment to the variable on the left side of the assignment operator. If you reverse the order, you'll confuse the C# compiler.

Fix

string firstName;
firstName = "Bob";

Stay Updated

Subscribe to our newsletter for the latest tutorials, tech insights, and developer news.

By subscribing, you agree to our privacy policy. Unsubscribe at any time.