C# Assignment Operators - Complete Guide
Introduction to Assignment Operators
Assignment operators in C# are used to assign values to variables. The most common assignment operator is the equals sign (=), but C# also includes compound assignment operators that combine arithmetic or logical operations with assignment.
These operators make code more concise and readable by reducing redundancy. A solid understanding of assignment operators is essential for writing clear and efficient C# programs.
Basic and Compound Assignment Operators
C# supports assignment operators for arithmetic, modulus, and string concatenation. Below are examples of their usage:
Example
using System;
namespace AssignmentOperatorsExample
{
class Program
{
static void Main(string[] args)
{
int x = 10;
Console.WriteLine($"Basic assignment: x = {x}");
x += 5; // Equivalent to x = x + 5
Console.WriteLine($"Addition assignment: x += 5 → {x}");
x -= 3; // Equivalent to x = x - 3
Console.WriteLine($"Subtraction assignment: x -= 3 → {x}");
x *= 2; // Equivalent to x = x * 2
Console.WriteLine($"Multiplication assignment: x *= 2 → {x}");
x /= 4; // Equivalent to x = x / 4
Console.WriteLine($"Division assignment: x /= 4 → {x}");
x %= 3; // Equivalent to x = x % 3
Console.WriteLine($"Modulus assignment: x %= 3 → {x}");
x = 10;
Console.WriteLine($"\nReset x to: {x}");
x += 3 * 2; // Equivalent to x = x + (3 * 2)
Console.WriteLine($"x += 3 * 2 → {x}");
double y = 15.5;
y /= 2.5;
Console.WriteLine($"y /= 2.5 → {y}");
string message = "Hello";
message += " World";
Console.WriteLine($"message += ' World' → {message}");
}
}
}
Output
Basic assignment: x = 10 Addition assignment: x += 5 → 15 Subtraction assignment: x -= 3 → 12 Multiplication assignment: x *= 2 → 24 Division assignment: x /= 4 → 6 Modulus assignment: x %= 3 → 0 Reset x to: 10 x += 3 * 2 → 16 y /= 2.5 → 6.2 message += ' World' → Hello World
Multiple Assignment and Chaining
C# also allows chaining assignment operations, applying them across multiple variables or within more complex expressions:
Example
using System;
namespace MultipleAssignmentExample
{
class Program
{
static void Main(string[] args)
{
int a, b, c;
a = b = c = 100;
Console.WriteLine($"a = {a}, b = {b}, c = {c}");
int x = 5, y = 10, z = 15;
x += 2; // 7
y -= 3; // 7
z *= 2; // 30
Console.WriteLine($"x = {x}, y = {y}, z = {z}");
int total = 0;
total += 10 * 2; // 20
total -= 5; // 15
total *= 3; // 45
total /= 5; // 9
Console.WriteLine($"Final total: {total}");
int[] numbers = { 1, 2, 3, 4, 5 };
int sum = 0;
for (int i = 0; i < numbers.Length; i++)
{
sum += numbers[i];
}
Console.WriteLine($"Sum of array: {sum}");
int result = 0;
result = (result + 5) * 2;
Console.WriteLine($"Result: {result}");
string name = null;
name ??= "Default Name";
Console.WriteLine($"Name: {name}");
name ??= "Another Name";
Console.WriteLine($"Name after second assignment: {name}");
}
}
}
Output
a = 100, b = 100, c = 100 x = 7, y = 7, z = 30 Final total: 9 Sum of array: 15 Result: 10 Name: Default Name Name after second assignment: Default Name