14. Interfaces in C#
Level: Beginner
- What an interface is
- Why interfaces are useful
- Create a simple interface
- Make a class implement an interface
- Understand interface as a promise
- Use interfaces in School Management examples
In the previous article, you learned abstract classes.
Now we learn interfaces.
An interface is a promise.
It says:
Any class that uses me must provide these methods.
The Problem: Different Classes Need the Same Ability
In a School Management System:
- Student report can be printed
- Teacher report can be printed
- Fee receipt can be printed
These are different classes, but they all have one common ability:
They can be printed.
An interface is perfect for this.
Quick Definitions
- Interface - A contract that lists required methods
- Contract - A promise a class must follow
- Implement - Write the methods required by an interface
- Capability - Something a class can do, like print or export
- Interface name - Usually starts with
I, likeIPrintable
Step 1: Create an Interface
public interface IPrintable
{
void Print();
}
Meaning:
Any class that implements IPrintable must have a Print() method.
The interface does not say how to print.
It only says:
Print() must exist.
Step 2: Implement the Interface
public class StudentReport : IPrintable
{
public string StudentName { get; set; } = "";
public void Print()
{
Console.WriteLine($"Printing report for {StudentName}");
}
}
StudentReport : IPrintable means:
StudentReport promises to provide Print().
Step 3: Another Class Can Use the Same Interface
public class FeeReceipt : IPrintable
{
public decimal Amount { get; set; }
public void Print()
{
Console.WriteLine($"Printing fee receipt for {Amount}");
}
}
Now both classes can be printed.
Full Example: Printable Reports
Expected output:
Student Report: Sahasra Kumar
Fee Receipt: Sahasra Kumar, Amount: 15000.00
Interface vs Abstract Class
| Use | Choose |
|---|---|
| Need shared data and shared code | Abstract class |
| Need only a required ability | Interface |
| A class should follow one parent type | Abstract class |
| Many different classes need same ability | Interface |
Simple memory trick:
Abstract class = common family
Interface = common ability
When You'll Use This in SMS
Interfaces help define common abilities:
IPrintable -> report or receipt can print
IExportable -> report can export
INotifiable -> class can send notification
Examples:
- Student report implements
IPrintable - Fee receipt implements
IPrintable - Attendance report implements
IExportable - SMS service implements
INotifiable
Try This Now
Run the full example. Then experiment:
- Add a
TeacherReportclass - Make it implement
IPrintable - Add a
Print()method - Create an object and call
Print()
C# interfaces video coming soon on NexCoding YouTube. Subscribe for updates.
Common Mistakes
Mistake 1: Creating an interface but not implementing all methods
If interface has Print(), the class must write Print().
Mistake 2: Putting too many methods in one interface
Keep interfaces small and focused.
Mistake 3: Thinking interface stores data
For beginners, think of an interface as a list of required actions.
Practice Task
Create an interface named IExportable with:
void Export();
Then create:
StudentReportAttendanceReport
Both should implement IExportable.
Quick Revision
| Question | Answer |
|---|---|
| What is an interface? | A contract or promise |
| What does implement mean? | Write required methods |
| What prefix is common for interface names? | I |
| Interface is best for what? | Common ability |
| Can many classes implement same interface? | Yes |
An interface is a contract. It lists methods a class must provide.
Implement means the class writes the methods required by the interface.
Use an interface when different classes need the same ability, such as printing or exporting.
Use ChatGPT, Claude, or Copilot to go deeper on C# interfaces. Try these prompts:
"Explain C# interfaces like I am a beginner""Give me 5 interface practice tasks""Explain interface vs abstract class with school examples""Quiz me with 5 beginner questions about interfaces"
💡 Tip: After reading this article, paste your own code into AI and ask "What could go wrong here and why?" — fastest way to find edge cases and deepen understanding.