Static vs instance
“What is the difference between a static and an instance member?”
Instance: one copy in every object. Static: one copy on the class, shared by all.
See it
In plain words
An instance member belongs to one object: every loan has its own Rate. A static member belongs to the class itself, so there is only one copy, shared by every object. Static is right for a formula that needs no data of its own. It is wrong for data that differs per loan: one loan changes it, and every loan sees the change.
- Instance members belong to an object; each instance has its own copy of instance fields, and instance methods run with (the object the method was called on).
- Static members belong to the type: one copy for the whole app, shared by every instance, request and thread.
- A static method has no , so it can't touch instance members without being given an object.
- A can't be instantiated and holds only static members.
- Use static for stateless helpers and constants; avoid mutable static fields, because they are shared across all users, not -safe, and hard to test.
In the lending API
A static helper, the bug, the fixed Loan, and the test that catches it. Press Next line to walk through them.
Lending.sln · where these 4 files sit
- src/Domain/
- Common/
- Servicing/
- Common/
- tests/Domain.UnitTests/Servicing/
static class Emi holds only the formula. No , no object: you call it on the class name.new Emi() is allowed, and every member must be static. It is a home for a formula, not a thing with values.What they ask next
Interviewers check that you can explain every line. Tap a question to open it.
ReportedWhy can't a controller action method be static?
ASP.NET Core builds a new controller object for each request and calls the action on it. Static methods are skipped when it looks for actions. An action also needs instance things: the services passed into the constructor, and and for this request. Minimal API handlers can be static; controller actions can't.
When is the controller object created?
Once per request, after routing picks the action. The fills its constructor. See .
Explain this lineCan a static method read
Rate?Not on its own. A static method has no , so it doesn't know which loan you mean. The compiler says CS0120: an object reference is required. Pass the loan, or its values, in as parameters, as does.
InterviewerStatic class or singleton: which, and why?
A singleton is one object, shared by the whole app. Unlike a static class, it can implement an , be passed in, and be swapped for a fake in a test. A static class can do none of that.
How do you make a singleton in ASP.NET Core?
Register it with and let the build one object and hand it out. Don't write a hand-made static
Instanceproperty.So when is a static class right?
For pure helpers with no state and nothing to fake: maths like , extension methods, constants.
What if two requests run at the same time?
They share every static field. If both write it, one can overwrite the other mid-calculation: a race. This is a problem, on its own page.
How long does a static field live?
As long as the app. The garbage collector treats a static field as a root: what it points to stays alive until the app stops or the field is cleared. A static list that only grows is a classic memory leak. See .
Why not…
constorstatic readonly?Both are one shared value that can't change.
constis copied into the calling code when it compiles, so it is only for values that never change, like 12 months in a year.static readonlyis set once at runtime and can hold any type, for example a . Trap: change a publicconstin a library, and other projects keep the old value until they are recompiled.Why do static fields make tests flaky?
xUnit runs test classes in parallel, in no fixed order. A static field set by one test is still there in the next. Tests pass alone and fail together.
Is static the same as stateless?
No. A static method with no static fields is stateless. A static field is , shared by everyone. That is the dangerous kind.
Traps
- Don't say
Static means the value can't change.SayStatic means one shared copy, owned by the class.constand stop change, and only to the field: astatic readonly List<T>still accepts Add(). - Don't say
Each request or user gets its own static fields.SayThere is one copy for the whole app, shared by every request and user. - Don't say
Make methods static because static calls are faster.SayThe speed gain is tiny. Static calls can't be faked in tests, so keep logic that needs swapping in instance classes.
A common AI-era interview question: "AI generated this snippet. How do you check it is safe, correct and efficient?" For any field in code you accept, ask one thing: should every loan, request and user share this value? If not, it can't be static.