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
int 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.
- Value types (
int,decimal,bool, , anystructorenum) hold their data directly, so assignment copies the data. - A
structis a type you define whose variables hold the data itself. - Reference types (
class,string, arrays,record class) hold a reference to an object on the managed heap, so assignment copies the reference and both variables see the same object. - A is a class or struct that compares by values.
- For a class, allocates the object and returns its reference.
- When nothing references it any more, the reclaims it later, at a time it chooses.
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/
int months = 60; The variable holds the value 60 itself. int is a value type.int 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.
Why not…So is a class passed to a method "by reference"?
No. C# passes every argument by value unless you write
ref. For a class, the value copied is the address. So the method can change the object, but if it points its parameter at a new object, the caller never sees that.What does
refdo, then?refpasses the caller's variable itself. Now the method can point the caller'scarat a different Loan, or change the caller'sint.outis the same, but the method must set it.
Explain this lineWhy and not ?
checks one thing: same object, same address. calls , which a class may change to compare values.
Samesays exactly what the test means.Explain this lineWhat is the
!incar!.Emi()?The null-forgiving operator. It only silences the compiler's "this may be null" warning. It does not stop the crash. Here the crash is the point of the test.
Is
stringa value type? It behaves like one.No,
stringis a reference type. It feels like a value because strings never change: every "change" builds a new string, so a shared one can't change under you.==on strings compares the text, not the address.What is boxing?
Putting a value type into an
objectvariable.object o = 60;copies the 60 into a new object on the heap.(int)ocopies it back out, which is unboxing. Each box is an allocation, so busy loops avoid it.When would you write a
structinstead of aclass?For a small value with no identity that rarely changes, like a coordinate. Microsoft's guideline: under about 16 bytes, immutable, and not boxed often. Anything with an identity and a life, like a Loan, is a class.
Two loans with the same values: are they equal?
Not with
==on a class: it compares addresses, and two calls give two addresses. A compares values instead, so two records with the same values are equal.
Traps
- Don't say
Value types live on the stack, reference types on the heap.SayObjects live on the heap. A value lives where it is declared: a localintusually on the stack, but theMonthsfield of a Loan sits on the heap, inside the Loan. - Don't say
A class is passed to a method by reference.SayEverything is passed by value unless you writeref. For a class, the value that gets copied is the address. - Don't say
SayIt copies the address. Both names reach one loan. A second loan needs .var preview = loan;makes a copy of the loan.
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?