Understanding Mutability in Data Types

Introduction

In programming, mutability refers to whether or not the value of an object can be changed after it is created.

  • Mutable → Can be changed after creation

  • ImmutableCannot be changed once created (a new object is created instead)

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:

  • The same numbers object is changed in memory.

  • No new list is created.

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:

  • A new string "Raj Bhatt" was created.

  • The original "Raj" string remains unchanged.

Common Immutable Types in C#

  • string

  • int, double, decimal, bool

  • DateTime

  • Guid

  • Tuple

  • Record types (introduced in C# 9+)

Mutable vs Immutable — Key Differences

FeatureMutableImmutable
State ChangeCan change internal dataCannot change after creation
Memory UsageUsually efficient (same object modified)May create new objects
Thread SafetyNot thread-safe by defaultNaturally thread-safe
ExamplesList, Dictionary, StringBuilderString, int, Record, DateTime
When to UseWhen frequent updates are neededWhen 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

  • Use readonly fields.

  • Set values only via constructor.

  • Do not expose setters.

  • Avoid methods that modify internal state.

Example

public class Person
{
    public string Name { get; }
    public int Age { get; }

    public Person(string name, int age)
    {
        Name = name;
        Age = age;
    }
}

Summary table

AspectMutableImmutable
Change allowed?YesNo
Thread-safe?NoYes
Memory efficient for updates?YesNo
Common ExamplesList, Array, StringBuilderString, 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!