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

Learn More About C# πŸ“š

1. What is C#? πŸ‘‰ Explained
2. Main Features of C# πŸ‘‰ Explained
3. Difference Between C# and Java πŸ‘‰ Explained
4. Common Language Runtime (CLR) in C# πŸ‘‰ Explained
5. Common Type System (CTS) in C# πŸ‘‰ Explained
6. Common Language Specification (CLS) in C# πŸ‘‰ Explained
7. Value Types vs Reference Types in C# πŸ‘‰ Explained
8. What is a Namespace in C#? πŸ‘‰ Explained
9. Purpose of the 'using' Keyword in C# πŸ‘‰ Explained
10. Different Data Types in C# πŸ‘‰ Explained
11. Difference Between int and Int32 in C# πŸ‘‰ Explained
12. Difference Between float, double, and decimal in C# πŸ‘‰ Explained
13. What is the Default Value of a Boolean in C#? πŸ‘‰ Explained
14. What is Boxing and Unboxing in C# πŸ‘‰ Explained
15. What are the Different Types of Operators in C# πŸ‘‰ Explained
16. Difference Between Equals and == in C# πŸ‘‰ Explained
17. What is the Null-Coalescing Operator ?? in C# πŸ‘‰ Explained
18. What is the Ternary Operator in C# πŸ‘‰ Explained
19. How Does the Switch Statement Work in C# πŸ‘‰ Explained
20. What is Object-Oriented Programming in C# πŸ‘‰ Explained
21. What are the Four Pillars of OOP in C# πŸ‘‰ Explained
22. What is Encapsulation in C# πŸ‘‰ Explained
23. What is Inheritance in C# πŸ‘‰ Explained
24. What is Polymorphism in C# πŸ‘‰ Explained
25. What is Abstraction in C# πŸ‘‰ Explained
26. What is an Abstract Class in C# πŸ‘‰ Explained
27. What is an Interface in C# πŸ‘‰ Explained
28. Can a Class Implement Multiple Interfaces in C#? πŸ‘‰ Explained
29. Difference Between Abstract Class and Interface in C# πŸ‘‰ Explained
30. How Do You Create a Class in C#? πŸ‘‰ Explained
31. What is a Constructor in C# πŸ‘‰ Explained
32. What Are the Types of Constructors in C# πŸ‘‰ Explained
33. What is a Static Constructor in C# πŸ‘‰ Explained
34. Difference Between Static and Non-Static Members in C# πŸ‘‰ Explained
35. What is the Use of 'this' Keyword in C# πŸ‘‰ Explained
36. What is a Destructor in C# πŸ‘‰ Explained
37. What is Object Initializer Syntax in C# πŸ‘‰ Explained
38. What is the Difference Between Field and Property in C# πŸ‘‰ Explained
Share:

Tags:


Feedback Modal Popup