Skip to main content

14. Interfaces in C#

Level: Beginner

ℹ️ What You'll Learn
  • 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, like IPrintable

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

💻 Try It — Console App
💡 Paste into Program.cs and press F5⌥ GitHub
public interface IPrintable
{
void Print();
}

public class StudentReport : IPrintable
{
public string StudentName { get; set; } = "";

public void Print()
{
Console.WriteLine($"Student Report: {StudentName}");
}
}

public class FeeReceipt : IPrintable
{
public string StudentName { get; set; } = "";
public decimal Amount { get; set; }

public void Print()
{
Console.WriteLine($"Fee Receipt: {StudentName}, Amount: {Amount}");
}
}

StudentReport report = new StudentReport
{
StudentName = "Sahasra Kumar"
};

FeeReceipt receipt = new FeeReceipt
{
StudentName = "Sahasra Kumar",
Amount = 15000.00m
};

report.Print();
receipt.Print();

Expected output:

Student Report: Sahasra Kumar
Fee Receipt: Sahasra Kumar, Amount: 15000.00

Interface vs Abstract Class

UseChoose
Need shared data and shared codeAbstract class
Need only a required abilityInterface
A class should follow one parent typeAbstract class
Many different classes need same abilityInterface

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:

  1. Add a TeacherReport class
  2. Make it implement IPrintable
  3. Add a Print() method
  4. Create an object and call Print()

ℹ️ Video Tutorial

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:

  • StudentReport
  • AttendanceReport

Both should implement IExportable.

Quick Revision

QuestionAnswer
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

🎯 Q1: What is an interface in C#?

An interface is a contract. It lists methods a class must provide.

🎯 Q2: What does implement mean?

Implement means the class writes the methods required by the interface.

🎯 Q3: When should we use an interface?

Use an interface when different classes need the same ability, such as printing or exporting.


🤖Use AI to Learn Faster
⚠️ Important for beginners: Do NOT use AI to write your code yet. Type every example yourself. Your brain learns by doing, not by reading AI output. Use AI only to explain and quiz you — not to code for you. Once you have strong fundamentals, AI becomes a powerful productivity tool for repetitive tasks.

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.

Next Article

Enums in C# ->

nexcoding.in