Interface
“What is an interface, and why would you use one?”
A contract: a list of what a class promises to have, with no code inside.
See it
In plain words
An interface is a contract. It lists what a class must have, names only, with no code inside. A class signs it, IndiaRules : ICountryRules, and writes the code. Loan only asks the contract how to round the , so India's rules today, or another country's later, plug in without changing Loan.
- An interface defines a contract: member signatures (methods, properties, events) with no implementation (since C# 8 it may have default methods, but never instance fields).
- A class implements it with
: ICountryRulesand must implement every member, or the build fails with CS0535. - You can't an interface, but a variable or parameter can have an interface type and hold any object that implements it.
- One call, different classes behind it: that is polymorphism.
- A class inherits one class but can implement many interfaces.
- Code that depends on the interface, not the concrete class, is loosely coupled: you can swap implementations, register them for , and replace them with fakes in tests.
In the lending API
The contract, the class that keeps it, Loan using it, and a test. Press Next line to walk through them.
Lending.sln · where these 4 files sit
- src/Domain/
- Common/Rules/
- India/
- India/
- Servicing/
- Common/Rules/
- tests/Domain.UnitTests/Servicing/
interface declares a contract, not a class. Starting the name with I is the .NET habit, so readers know at a glance.What they ask next
Interviewers check that you can explain every line. Tap a question to open it.
Why not…Interface or abstract class?
An abstract class is a half-built class: it can hold fields, a constructor and shared code, and a class can inherit only one. An interface is a contract only: no instance fields, no constructor, and a class can implement many. Use an interface for a role that unrelated classes can play, like a country's rules. Use an abstract class when closely related classes share real code and data.
Since C# 8 interfaces can have code. Is the difference gone?
No. Default interface methods exist mainly so a library can add a member without breaking every class that already implements it. An interface still can't hold instance fields or a constructor, so shared data still needs a class. A default method can be called only through the interface type, not through the class.
Explain this lineWhy pass the rules into , not the constructor?
Loan is an entity (a class saved as a database row): builds it when it loads a row, and has no rules object to hand it. Passing the rule into the method keeps Loan plain data plus logic. Services like LoanService are different: they get their interfaces through the constructor, from the .
How does the app pick IndiaRules?
One line in Program.cs:
builder.Services.AddSingleton<ICountryRules, IndiaRules>();. Any class that asks for gets the IndiaRules object. More on .Two classes implement one interface. Which one do you get?
The one registered last. Ask for to get all of them. Keyed services (.NET 8) pick one by a key:
AddKeyedSingleton<ICountryRules, IndiaRules>("IN"). The class receives it with[FromKeyedServices("IN")] ICountryRules rules.
How do interfaces help testing?
A test can pass in any class that keeps the contract, like . For outside systems, like the , teams use a mocking library such as NSubstitute:
Substitute.For<ICreditBureauClient>()builds a fake object while the test runs. Mocking libraries can fake an interface easily; a normal class only through its methods (methods a subclass may replace).Should every class have an interface?
No. Add one where something will be swapped or faked: outside systems, country rules, services. Data classes like Loan or a request DTO (a class that only carries data in or out of an API) don't need one. An interface with one class and no fake is just one more file to keep in step.
What is explicit interface implementation?
Writing the member with the interface's name in front:
decimal ICountryRules.RoundEmi(decimal emi). It can then be called only through a variable of the interface type. Used when two interfaces have a member with the same name and each needs different code.Can an interface have fields or a constructor?
No instance fields and no constructor. It can list properties, methods and events. Since C# 8 it can also have static members and default method bodies; C# 11 added
static abstractmembers.How do interfaces relate to SOLID?
Two of the five letters. I (interface segregation): keep interfaces small, so no class must write members it doesn't use. D (dependency inversion): Loan depends on ICountryRules, not on IndiaRules.
What is the strategy pattern?
Several classes keep one contract, and the one to use is picked while the app runs. ICountryRules is an example: the code that uses it stays the same while the rule changes.
Traps
- Don't say
An interface is a class with no code.SayIt is a different kind of type. You can't it, and one class can implement many interfaces. - Don't say
Every class needs an interface, for testing.SayAdd one where something will be swapped or faked. Data classes and DTOs don't need one.
Anthropic's guide for Claude Code suggests writing a spec that names the files and interfaces before the AI writes code. The interface is the part you design and must defend in a review; the class behind it is the part AI fills in fast. Expect "why this contract?" more than "write this class".