What are rvalue and lvalue in C#?

In C#, expressions are categorized into two types based on their position and usage in assignment operations: lvalue and rvalue. Understanding these concepts helps in writing correct assignment statements and understanding compiler errors.

Definitions

  • lvalue − An expression that represents a memory location and can appear on either the left-hand or right-hand side of an assignment. The term "lvalue" means "left value".

  • rvalue − An expression that represents a value and can only appear on the right-hand side of an assignment. The term "rvalue" means "right value".

lvalue vs rvalue in Assignment lvalue Memory location Can be assigned to = rvalue Value/Expression Cannot be assigned to variable 100 int x = 100; Valid: lvalue = rvalue

Examples of lvalues and rvalues

Valid Assignment with lvalue and rvalue

using System;

class Program {
    public static void Main() {
        int a = 100;  // 'a' is lvalue, '100' is rvalue
        int b = 50;   // 'b' is lvalue, '50' is rvalue
        
        a = b;        // Both 'a' and 'b' can be lvalues
        Console.WriteLine("a = " + a);
        
        a = a + 10;   // 'a' is lvalue, 'a + 10' is rvalue
        Console.WriteLine("a after adding 10 = " + a);
    }
}

The output of the above code is −

a = 50
a after adding 10 = 60

Invalid Assignments (Compilation Errors)

The following examples show what happens when you try to use rvalues as lvalues −

// These will cause compilation errors:
100 = a;        // Error: Cannot assign to '100' (literal)
a + b = 50;     // Error: Cannot assign to 'a + b' (expression)
GetValue() = 10; // Error: Cannot assign to method call (unless it returns a reference)

Common lvalues and rvalues

lvalue Examples rvalue Examples
Variables: int x Literals: 100, "Hello", true
Array elements: arr[0] Arithmetic expressions: x + y
Object properties: obj.Property Method calls: Math.Max(a, b)
Reference parameters: ref variable Temporary values: new Object()

Property and Array Examples

using System;

class Student {
    public string Name { get; set; }
}

class Program {
    public static void Main() {
        Student student = new Student();
        int[] numbers = new int[3];
        
        // Properties as lvalues
        student.Name = "John";  // student.Name is lvalue, "John" is rvalue
        Console.WriteLine("Student name: " + student.Name);
        
        // Array elements as lvalues
        numbers[0] = 10;       // numbers[0] is lvalue, 10 is rvalue
        numbers[1] = 20;       // numbers[1] is lvalue, 20 is rvalue
        numbers[2] = numbers[0] + numbers[1]; // numbers[2] is lvalue, expression is rvalue
        
        Console.WriteLine("Array values: " + numbers[0] + ", " + numbers[1] + ", " + numbers[2]);
    }
}

The output of the above code is −

Student name: John
Array values: 10, 20, 30

Conclusion

Understanding lvalues and rvalues in C# is essential for writing correct assignment statements. lvalues represent memory locations that can store values, while rvalues represent the actual values or expressions that evaluate to values. This distinction helps prevent compilation errors and improves code clarity.

Updated on: 2026-03-17T07:04:35+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements