← Foundations path
.NETFoundations 9 / 37

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 your codewritten onceIn memorymade while the app runsheapICountryRulesinterface: a contractCurrencyRoundEmi(emi)implementsIndiaRulesclass: has the codeCurrency: "INR"RoundEmi: up to ₹1PaiseRulesclass: test stand-inCurrency: "INR"RoundEmi: keeps paiseBuild fails: CS0535new ICountryRules()Build fails: CS0144Loan @ 0x1AAmount 500000Rate 9.5Months 60Emi(rules) = 10,501IndiaRules @ 0x3CCurrency INRPaiseRules @ 0x4DCurrency INR
1 / 8An interface is a contract: a list of what a class must have. Names only, no code inside.

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.

Say it in the interview

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/
    • Servicing/
  • tests/Domain.UnitTests/Servicing/
Diagram · step 1An interface is a contract: a list of what a class must have. Names only, no code inside.
In your codewritten onceIn memorymade while the app runsheapICountryRulesinterface: a contractCurrencyRoundEmi(emi)implementsIndiaRulesclass: has the codeCurrency: "INR"RoundEmi: up to ₹1PaiseRulesclass: test stand-inCurrency: "INR"RoundEmi: keeps paiseBuild fails: CS0535new ICountryRules()Build fails: CS0144Loan @ 0x1AAmount 500000Rate 9.5Months 60Emi(rules) = 10,501IndiaRules @ 0x3CCurrency INRPaiseRules @ 0x4DCurrency INR
ICountryRules.cs · line 4 · 1 of 3interface 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.

Traps

With AI tools

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".

Read more: Microsoft Learn: interfaces · The interface keyword · Explicit interface implementation · Default interface methods · DI: multiple and keyed services · Claude Code best practices