What is an Interface in C#?

๐Ÿ’ก Concept: Interface

An interface in C# defines a contract that classes must adhere to. It contains only declarations of membersโ€”such as methods, properties, and eventsโ€”without implementing any behavior (except default methods from C# 8.0+).

๐Ÿ“˜ Quick Intro

Interfaces enable polymorphism and code decoupling. A class can implement multiple interfaces, making them ideal for designing flexible systems, especially in testing, DI, and SOLID architecture.

๐Ÿง  Analogy

Imagine an interface as a job descriptionโ€”it lists what a person must be able to do (skills), but not how they do it. Different candidates (classes) can fulfill the same job (interface) in their unique ways.

๐Ÿ”ง Technical Explanation

  • Declared using the interface keyword.
  • Cannot contain fields or constructors.
  • All members are public and abstract by default (until C# 8+).
  • A class can implement multiple interfaces.
  • Interfaces support default implementations (C# 8.0+).

๐ŸŽฏ Use Cases

  • Defining shared behavior across unrelated classes.
  • Creating plug-and-play designs that rely on abstraction.
  • Enabling mocking and testing via dependency injection.
  • Applying SOLID principles, especially Interface Segregation.

๐Ÿ’ป Real Code Example

public interface IShape {
    double GetArea();
}

public class Circle : IShape {
    public double Radius { get; set; }
    public double GetArea() => Math.PI * Radius * Radius;
}

public class Square : IShape {
    public double Side { get; set; }
    public double GetArea() => Side * Side;
}

โ“ Interview Q&A

Q1: What is the purpose of an interface?
A: To define a contract of methods/properties a class must implement.

Q2: Can an interface contain fields?
A: No, only declarations (and default implementations from C# 8+).

Q3: Can a class implement multiple interfaces?
A: Yes, unlike classes, multiple interface inheritance is allowed.

Q4: Are interface methods public by default?
A: Yes, and cannot be made private.

Q5: Can an interface be instantiated?
A: No, only classes that implement it can be.

Q6: What is the use of interfaces in DI?
A: It allows substituting implementations easily for testing/mocking.

Q7: Can interfaces extend other interfaces?
A: Yes, using inheritance like: interface B : A.

Q8: Can interfaces have static members?
A: C# 8+ allows static members in interfaces.

Q9: Can interface methods be implemented directly?
A: No, unless using default implementation from C# 8+.

Q10: What is the difference between interface and abstract class?
A: Interfaces canโ€™t have fields and allow multiple implementations; abstract classes provide partial implementation and allow fields.

๐Ÿ“ MCQs

Q1. What does an interface define?

  • A constructor
  • A class
  • A contract
  • A data type

Q2. Which keyword defines an interface?

  • class
  • interface
  • contract
  • struct

Q3. Can a class implement multiple interfaces?

  • No
  • Yes
  • Only if sealed
  • Only if static

Q4. Are interface members public by default?

  • No
  • Yes
  • Private
  • Internal

Q5. What version added default implementations to interfaces?

  • C# 6.0
  • C# 7.0
  • C# 8.0
  • C# 9.0

Q6. Can an interface contain fields?

  • Yes
  • Only constants
  • No
  • Only readonly

Q7. What is the use of interfaces in testing?

  • Slows performance
  • Restricts access
  • Allows mocking
  • Creates hierarchy

Q8. Can interfaces extend other interfaces?

  • No
  • Yes
  • Only one
  • Only abstract ones

Q9. Which is true about interface instantiation?

  • Can be instantiated
  • Only once
  • Cannot be instantiated
  • Instantiated with 'new'

Q10. Which is a key benefit of interfaces?

  • Fields in interface
  • Encapsulation
  • Multiple inheritance
  • Internal usage only

๐Ÿ’ก Bonus Insight

Interfaces are essential for clean architecture and test-driven development. By abstracting functionality behind contracts, your application becomes easier to extend, refactor, and maintain.

๐Ÿ“„ PDF Download

Need a handy summary for your notes? Download this topic as a PDF!

๐Ÿ” Navigation

๐Ÿ’ฌ Feedback
๐Ÿš€ Start Learning
Share:

Tags: