Introduction
In programming, mutability refers to whether or not the value of an object can be changed after it is created.
Mutability affects memory usage, performance, and behavior—especially when passing objects between functions or working with collections.
Let’s explore what this means with examples .
What is a Mutable Type?
A mutable type is one whose internal state (its data) can be modified without creating a new object.
Example: Mutable Object (C# List)
List<int> numbers = new List<int> { 1, 2, 3 };
numbers.Add(4); // modifies the same list
Console.WriteLine(string.Join(", ", numbers)); // Output: 1, 2, 3, 4
Here:
Common Mutable Types in C#
List<T>
Dictionary<TKey, TValue>
Array
StringBuilder
What is an Immutable Type?
An immutable type is one whose value cannot change once it’s created.
If you try to modify it, a new object is created instead.
Example: Immutable Object (C# String)
string name = "Raj";
name += " Bhatt";
Console.WriteLine(name); // Output: Raj Bhatt
But internally:
Common Immutable Types in C#
string
int, double, decimal, bool
DateTime
Guid
Tuple
Record types (introduced in C# 9+)
Mutable vs Immutable — Key Differences
| Feature | Mutable | Immutable |
|---|
| State Change | Can change internal data | Cannot change after creation |
| Memory Usage | Usually efficient (same object modified) | May create new objects |
| Thread Safety | Not thread-safe by default | Naturally thread-safe |
| Examples | List, Dictionary, StringBuilder | String, int, Record, DateTime |
| When to Use | When frequent updates are needed | When consistency and safety matter |
Example: Why Mutability Matters
Mutable – StringBuilder
StringBuilder sb = new StringBuilder("Hello");
sb.Append(" World");
Console.WriteLine(sb); // Output: Hello World
Only one object is used and modified — memory efficient.
Immutable – String
string s = "Hello";
s += " World";
Console.WriteLine(s); // Output: Hello World
A new string object is created for "Hello World" — less memory efficient in loops or concatenations.
Common Interview Questions
Q 1: What is the difference between mutable and immutable types?
Ans: Mutable objects can be changed after creation; immutable objects cannot be changed once created — new objects are formed when modified.
Q 2: Why are strings immutable in C#?
Ans: To ensure security, thread safety, and performance optimization (e.g., string interning, hash consistency).
Q 3: What happens when you modify an immutable object like string?
Ans: A new object is created in memory, and the old one remains until garbage collected.
Q 4: Which type is preferred in multi-threaded environments?
Ans: Immutable types — they avoid race conditions since their data cannot change.
Q 5: How can you make your own class immutable?
Ans
Example
public class Person
{
public string Name { get; }
public int Age { get; }
public Person(string name, int age)
{
Name = name;
Age = age;
}
}
Summary table
| Aspect | Mutable | Immutable |
|---|
| Change allowed? | Yes | No |
| Thread-safe? | No | Yes |
| Memory efficient for updates? | Yes | No |
| Common Examples | List, Array, StringBuilder | String, int, DateTime |
Conclusion
Understanding mutability and immutability is more than just a theoretical concept — it shapes how you design, optimize, and secure your applications.
By knowing which data types can or can’t change, you can:
Avoid unexpected side effects,
Improve performance, and
Write code that’s predictable, safe, and efficient.
Thank you for taking the time to read this post. I hope it has given you a clear and practical understanding of mutable and immutable data types, their differences, and where to apply each concept in real-world C# development.
Keep learning and keep building robust, efficient, and maintainable applications!