Class and object
“What is the difference between a class and an object?”
The blueprint you write once, and the things built from it while the app runs.
See it
In plain words
A class is a blueprint you write once in code. An object is a real thing built from that blueprint while the app runs. One class can build many objects, and each keeps its own values. is the class. Your car loan and your bike loan are two objects.
- A class is a type definition: it declares the properties and methods its objects will have.
- An object, also called an instance, is built from that class with while the app runs.
- It normally lives on the , holds its own copy of the instance data, and code reaches it through a .
In the lending API
Two real files from the lending API. Press Next line to walk through them.
Lending.sln · where these 2 files sit
- src/Domain/Servicing/
- tests/Domain.UnitTests/Servicing/
class Loan declares a new type. No loan exists yet. This only describes what a loan will look like.What they ask next
Interviewers check that you can explain every line. Tap a question to open it.
Explain this lineWhy and not a public setter?
So only Loan's own code can change its data. Outside code must call a method like , and that method can refuse bad values. This is called encapsulation.
Why not…Why
decimalfor money, notdouble?doublestores numbers in binary, so 0.1 + 0.2 is not exactly 0.3.decimalstores base-10 digits, so rupees and paise stay exact. In C#, money is alwaysdecimal.Then why not use decimal everywhere?
It is slower and takes 16 bytes instead of 8. Use
doublefor science, graphics and measurements, where a tiny rounding error does not matter.
Explain this lineWhy check for 0 in the constructor?
divides by (f − 1). With a rate of 0, f stays 1 and that is 0, so it throws . Without the check the bad loan is created fine, and the crash comes later, far from the cause. The check makes a Loan with a bad rate impossible. More on the page.
Why not…Why pass ?
uses banker's rounding by default: 2.345 becomes 2.34, because halves go to the even digit. For amounts shown to customers, a team usually picks away-from-zero (2.35) and uses it everywhere.
Where does the object live in memory?
Normally on the managed heap. The variable
carholds a , which is the object's address, not the object itself.What about an
intor astruct?Those are value types: the variable holds the value itself, not an address. The difference is on the page.
Can two variables point to the same object?
Yes.
var other = car;copies the address, not the object. A change made throughotheris seen throughcartoo.
What does mean, then?
A static member belongs to the class itself, not to any object. There is one copy, shared by all code. See .
Is a class the same thing as a type?
A class is one kind of type. C# also has struct, record, interface and enum. Each describes a kind of value.
Traps
- Don't say
A class uses memory for its fields as soon as you write it.SayEach object's data uses memory only when that object is created with . The class is the description. - Don't say
A class and an object are the same thing.SayThe class is the type. An object is one instance of it, made while the app runs.
AI tools write a class like Loan in seconds. Interviewers now ask "why this and not that" about each line, for example why or why decimal. In the research for this app, a smooth first answer that falls apart on that question was the strongest rejection signal.