← Foundations path
.NETFoundations 2 / 37

References and new

“What is the difference between a value type and a reference type?”

Some variables hold a value. Others hold only an address, and copying one never copies the object.

See it

Variablesa value, or an addressObjectsmade by newstackheapmonths = 60m2 = 60car = 0x1Asame = 0x1ALoan @ 0x1AAmount 500000Rate 9.5Months 60freed by the GC
1 / 9int months = 60; The variable holds the value 60 itself. int is a value type.

In plain words

Some variables hold the value itself, like int months = 60. These are value types. Other variables hold only the address of an object that lives somewhere else in memory, like var car = new Loan(...). These are reference types. Copy a value and you get two separate values. Copy a reference and you get two names for the same object. The stack is a method’s short-lived scratch space for its local variables. The heap is where objects made by live until the garbage collector, .NET’s automatic memory cleaner, frees them.

Say it in the interview

In the lending API

One real test file from the lending API. The last test is a production bug and its fix. Press Next line to walk through it.

Lending.sln · where this file sits
  • tests/Domain.UnitTests/Servicing/
Diagram · step 1int months = 60; The variable holds the value 60 itself. int is a value type.
Variablesa value, or an addressObjectsmade by newstackheapmonths = 60m2 = 60car = 0x1Asame = 0x1ALoan @ 0x1AAmount 500000Rate 9.5Months 60freed by the GC
ReferenceTests.cs · line 10 · 1 of 15int is a value type. The variable months holds the number 60 itself, not an address.

What they ask next

Interviewers check that you can explain every line. Tap a question to open it.

Traps

With AI tools

Canva's interview guide says it grades whether you find and fix the issues in AI-written code. var preview = loan; is a typical find-the-bug line: it compiles, it looks fine, and it changes the real loan. Spot it by asking of every assignment: does this copy a value or an address?

Read more: Microsoft Learn: value types · Microsoft Learn: reference types · Passing parameters, ref and out · Choosing between class and struct · Nullable reference types · Canva: AI in our interviews