C#
Reference Type
Programming
.NET
Software Development

Reference type in C

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

In C#, a reference type variable stores a reference to an object rather than embedding the full object data directly in the variable itself. Understanding that model is essential because assignment, mutation, null handling, and parameter passing all behave differently from value types.

Common Reference Types in C#

Classes, arrays, delegates, interfaces, and string are reference types. When you create one of these objects, the variable points to that object rather than containing the entire object inline.

Here is a simple class example:

csharp
1public class Person
2{
3    public string Name { get; set; } = "";
4}
5
6var first = new Person { Name = "Ava" };
7var second = first;
8
9second.Name = "Lina";
10
11Console.WriteLine(first.Name); // Lina

After the assignment second = first, both variables refer to the same Person object. Changing the object's state through one variable is visible through the other.

Compare That with a Value Type

A value type copies its data on assignment. A struct example makes the contrast clear:

csharp
1public struct Point
2{
3    public int X;
4    public int Y;
5}
6
7var a = new Point { X = 1, Y = 2 };
8var b = a;
9
10b.X = 99;
11
12Console.WriteLine(a.X); // 1

Here b receives an independent copy of the data. Mutating b does not change a.

Reference Type Does Not Mean "Passed by Reference"

This is a subtle but important point. A reference type variable contains a reference, but that reference is still passed by value unless you use ref, out, or in.

csharp
1public class Counter
2{
3    public int Value { get; set; }
4}
5
6static void Reassign(Counter counter)
7{
8    counter = new Counter { Value = 100 };
9}
10
11var c = new Counter { Value = 5 };
12Reassign(c);
13
14Console.WriteLine(c.Value); // 5

The method received a copy of the reference. Reassigning that local parameter did not replace the caller's variable. If the method had mutated counter.Value instead, the caller would have observed the change.

string Is a Reference Type but Behaves Differently

string often confuses people because it is a reference type but immutable. Two variables can refer to the same string object, yet you cannot mutate that object in place.

csharp
1string x = "hello";
2string y = x;
3
4y = y.ToUpper();
5
6Console.WriteLine(x); // hello
7Console.WriteLine(y); // HELLO

ToUpper() returned a new string rather than changing the original one. That is why strings feel different from mutable class instances.

Nullability Matters

Reference types can be null, which is why null checks matter. Modern C# adds nullable reference types to help the compiler warn you about unsafe null usage.

csharp
1string? maybeName = null;
2
3if (maybeName is null)
4{
5    Console.WriteLine("No name yet");
6}

Enabling nullable reference types makes many bugs easier to catch before runtime.

Common Pitfalls

The first pitfall is assuming assignment copies the whole object for reference types. It usually copies only the reference, so both variables point to the same object.

Another is believing that all reference types are mutable like classes. Strings are reference types, but they are immutable.

A third mistake is confusing "reference type" with "pass by reference." Methods still receive a copy of the reference unless the parameter explicitly uses ref, out, or in.

Finally, avoid oversimplified memory rules such as "reference types are always on the heap and value types are always on the stack." The runtime implementation is more nuanced than that, and the semantic behavior of the type is usually what matters to application code.

Summary

  • Reference type variables hold references to objects rather than inline copies of object state.
  • Assigning one reference variable to another usually makes both variables point to the same object.
  • Value types copy their data on assignment, which is why their mutation behavior differs.
  • 'string is a reference type, but it is immutable.'
  • Reference types can be null, and nullable reference types help catch mistakes earlier.

Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.