Data Types in C# with Examples: Value and Reference Data Type

Learn about value and reference data types in C# with clear examples, and understand how each behaves differently in memory.

What is a Data Type?

A data type defines the kind of value a variable can hold and the operations that can be performed on it. C# is a strongly typed language, meaning every variable must have a defined type at compile time.

Value Data Types

Value types, such as int, float, bool, char, and struct, store their actual data directly in the variable itself. When assigned to another variable, the value is copied, so changes to one don't affect the other.

Reference Data Types

Reference types, such as class, string, array, and delegate, store a reference to the memory location where the actual data lives. Assigning one reference variable to another means both point to the same underlying object.

Key Differences in Behavior

Because value types are copied and reference types are shared, understanding this distinction is critical when passing data to methods or working with collections, as it directly affects how changes propagate through your program.

Conclusion: Grasping the difference between value and reference data types is one of the most important foundational concepts in C#, and it shapes how you reason about memory, performance, and program correctness.

Seeing the Copy Difference in Code

The clearest way to understand value and reference types is to run a tiny program. A struct and a class with the same field behave differently after assignment.

struct PointS { public int X; }
class  PointC { public int X; }

var s1 = new PointS { X = 1 };
var s2 = s1;        // copies the value
s2.X = 99;
Console.WriteLine(s1.X);   // 1

var c1 = new PointC { X = 1 };
var c2 = c1;        // copies the reference
c2.X = 99;
Console.WriteLine(c1.X);   // 99

The same rule applies when you pass arguments to methods. A method that changes a struct parameter changes its own copy; a method that changes a class parameter's field changes the caller's object. To let a method modify a value type variable directly, pass it with ref.

Built-In Types You Use Every Day

TypeSizeTypical use
int32-bitCounters, IDs, quantities
long64-bitLarge counts, timestamps
double64-bit floating pointScientific and measurement values
decimal128-bitMoney, where exact decimal digits matter
booltrue or falseFlags and conditions
char16-bit UTF-16 unitSingle characters
stringReference typeText

Use decimal for currency. Binary floating types like double cannot store values such as 0.1 exactly, so 0.1 + 0.2 == 0.3 is false for doubles but true for decimals written as 0.1m + 0.2m.

Special Cases Worth Knowing

Strings are references that act like values

string is a reference type, but it is immutable. Methods like ToUpper() return a new string instead of changing the original, so two variables sharing a string can never affect each other.

Nullable value types

A plain int cannot be null. Adding a question mark, int?, allows "no value", which is useful for optional database columns. Read it safely with age ?? 0 or check age.HasValue.

Boxing and unboxing

Assigning a value type to object wraps it in a heap object (boxing); casting back unwraps it (unboxing). Doing this in tight loops costs performance, which is one reason generic collections like List<int> replaced the old ArrayList.

int count = 5;
object boxed = count;      // boxing
int back = (int)boxed;     // unboxing
int? maybe = null;
Console.WriteLine(maybe ?? -1);   // -1

Mistakes to Avoid

  • Assuming value types always live on the stack. A value-type field inside a class lives on the heap with that object; the real difference is copy behaviour.
  • Creating large mutable structs, which are expensive to copy and confusing to update. Keep structs small and ideally readonly.
  • Comparing floating-point values with ==; compare within a small tolerance instead.
  • Ignoring overflow: adding 1 to an int variable that already holds int.MaxValue wraps around to a large negative number silently, unless the code runs inside a checked block.

Ready to Master C# Programming Course?

Join Uncodemy's hands-on training program with expert mentors and real-world projects.