Published on

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

Authors

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";