Structs, Classes, and the new Keyword
The C# Misconception Every Developer Gets Wrong
It’s one of the most misunderstood aspects of C# — and the answer might surprise you.
Every C# developer encounters this moment of confusion:
“Wait… if structs are value types that live on the stack, why am I using new to create them? Isn’t new for heap allocation?”
Let’s clear it up once and for all.
The Misconception
Many developers carry this mental model from languages like C or C++:
new = heap allocation = reference types only
In C#, this is incorrect. The new keyword doesn’t mean “allocate on the heap.” It means “call the constructor to initialize this object.” Where the memory lives is a completely separate concern.
Classes vs Structs: A Quick Refresher
// Reference type - lives on the heap
public class EmployeeClass
{
public string Name;
public int Id;
}// Value type - lives on the stack
public struct EmployeeStruct
{
public string Name;
public int Id;
}Here’s how they compare:
Class:
- Type category: Reference type
- Memory: Heap
- Default value: null
- Assignment: Copies the reference
Struct:
- Type category: Value type
- Memory: Stack (or inline within the containing object)
- Default value: All fields zeroed
- Assignment: Copies the entire value
What new Actually Does
Here’s the key insight — new behaves differently depending on the type:
For a Class → Allocates memory on the heap + calls constructor
For a Struct → Calls constructor only — no heap allocation
For structs, the memory already exists on the stack the moment you declare the variable. new simply initializes it.
You Don’t Need new for Structs
This is the part that surprises most people. Consider this struct:
public struct Point
{
public int X;
public int Y;
}You can absolutely use it without new:
Point p; // memory is allocated on the stack right here
p.X = 10; // assign each field manually
p.Y = 20;
Console.WriteLine($"({p.X}, {p.Y})"); // (10, 20)No new. No heap. It just works.
So Why Do People Use new With Structs?
Convenience. Without new, the compiler enforces a strict rule: you must assign every field before you read the struct.
Point p;
Console.WriteLine(p.X); // Compile error: Use of unassigned variable ‘p’
Even assigning some fields isn’t enough:
Point p;
p.X = 10;
Console.WriteLine(p.X); // Compile error: ‘p’ is not fully assignedYou must assign all fields:
Point p;
p.X = 10;
p.Y = 20;
Console.WriteLine(p.X); // Now it compilesUsing new zero-initializes everything in one shot:
Point p = new Point(); // X = 0, Y = 0 - fully assigned, ready to use
Console.WriteLine(p.X); // Works immediatelyThat’s it. That’s the reason. new on a struct is a shortcut for “set all fields to their default values.”
With Custom Constructors
Structs can have parameterized constructors just like classes:
public struct Point
{
public int X;
public int Y;
public Point(int x, int y)
{
X = x;
Y = y;
}
}
Point p = new Point(10, 20); // calls constructor - still NO heap allocationThe new here calls your constructor, but the Point still lives on the stack.
Proof: Visualizing Memory
void Demo()
{
Point s = new Point(5, 10); // struct - value type
EmployeeClass c = new EmployeeClass(); // class - reference type
}Here’s what memory actually looks like:
The struct s lives entirely on the stack. The class c has a reference on the stack pointing to the object on the heap.
Classes MUST Use new
Unlike structs, you cannot use a class without new:
// Struct — valid without new
Point p;
p.X = 10;
p.Y = 20; // ✓
// Class — NOT valid without new
EmployeeClass e;
e.Name = “Kumar”; // ❌ Compile error — no object exists yetA class variable without new is just a null reference. There’s no object in memory to write to. new is mandatory because it creates the object on the heap.
Assignment Behavior: The Real Difference
This is where the value-type vs reference-type distinction truly matters.
Struct (value type) — copies the data:
Point a = new Point(10, 20);
Point b = a; // copies all values into b
b.X = 99;
Console.WriteLine(a.X); // 10 - unchanged
Console.WriteLine(b.X); // 99 - independent copy
Class (reference type) — copies the reference:
EmployeeClass a = new EmployeeClass { Name = “Alice” };
EmployeeClass b = a; // copies the REFERENCE — both point to same object
b.Name = “Bob”;
Console.WriteLine(a.Name); // Bob ← changed!
Console.WriteLine(b.Name); // Bob ← same objectCommon Interview Question
“When should you use a struct instead of a class?”
Use a struct when:
- The type is small (Microsoft recommends under 16 bytes)
- It’s logically a single value (like Point, Color, DateTime)
- It’s immutable or short-lived
- You want to avoid heap allocations for performance
Use a class for everything else.
The new keyword in C# is not about where memory is allocated. It’s about calling a constructor to initialize an object. Structs already have their memory on the stack — new just makes sure all fields start with known values.
Next time someone asks why structs use new, you’ll know: it’s not allocation, it’s initialization.
. . .
Found this helpful? Give it a clap and share it with a fellow .NET developer who’s been confused by this too.
Follow me for more C# and .NET deep dives.





