Reference type in C
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
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:
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:
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.
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.
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.
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.
- '
stringis a reference type, but it is immutable.' - Reference types can be
null, and nullable reference types help catch mistakes earlier.
Related reading
- Referencing 2 different versions of log4net in the same solution
- Refresh DataGridView when updating data source
- Regarding usage of Task.Start , Task.Run and Task.Factory.StartNew
- Register for COM Interop vs Make assembly COM visible
- Reinforcement learning in C
- Release generating .pdb files, why?
- Reliably stop System.Threading.Timer?
- Remove characters from C string

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack 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.