Interfaces help different parts of a program work together by agreeing on what methods they must have. They make code easier to change and reuse.
Why interfaces are needed in C Sharp (C#)
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
C Sharp (C#)
interface IExample { void DoWork(); }
An interface lists methods without code.
Classes that use the interface must write the method code.
Examples
C Sharp (C#)
interface IAnimal { void Speak(); }
C Sharp (C#)
class Dog : IAnimal { public void Speak() { Console.WriteLine("Woof!"); } }
Sample Program
This program shows two different workers doing their own work but both use the same interface. This way, the main program can treat them the same.
C Sharp (C#)
using System; interface IWorker { void Work(); } class Teacher : IWorker { public void Work() { Console.WriteLine("Teaching students."); } } class Engineer : IWorker { public void Work() { Console.WriteLine("Building software."); } } class Program { static void Main() { IWorker worker1 = new Teacher(); IWorker worker2 = new Engineer(); worker1.Work(); worker2.Work(); } }
Important Notes
Interfaces do not contain any code, only method signatures.
One class can implement many interfaces to share different behaviors.
Interfaces help make programs flexible and easier to change later.
Summary
Interfaces define a contract for what methods a class must have.
They allow different classes to be used in the same way.
Interfaces make code easier to maintain and extend.
Practice
1. Why do we use interfaces in C# programming?
easy
Solution
Step 1: Understand the purpose of interfaces
Interfaces specify methods that a class must implement, acting like a contract.Step 2: Compare other options
Options B, C, and D describe unrelated concepts: data storage, UI design, and comments.Final Answer:
To define a contract that classes must follow -> Option AQuick Check:
Interface purpose = contract [OK]
Hint: Interfaces define required methods, not data or UI [OK]
Common Mistakes:
- Confusing interfaces with classes
- Thinking interfaces store data
- Mixing interfaces with UI design
2. Which of the following is the correct way to declare an interface in C#?
easy
Solution
Step 1: Identify interface syntax
In C#, interfaces are declared using the keywordinterfacefollowed by the name and method signatures.Step 2: Check other options
Options A, B, and C use enum, class, and struct keywords, which are not for interfaces.Final Answer:
interface IExample { void DoWork(); } -> Option CQuick Check:
Interface keyword = interface declaration [OK]
Hint: Look for 'interface' keyword to declare interfaces [OK]
Common Mistakes:
- Using class or struct instead of interface
- Missing method signature semicolon
- Confusing enums with interfaces
3. What will be the output of this C# code?
interface IAnimal { void Speak(); }
class Dog : IAnimal { public void Speak() { Console.WriteLine("Woof"); } }
class Cat : IAnimal { public void Speak() { Console.WriteLine("Meow"); } }
static void Main() {
IAnimal animal = new Dog();
animal.Speak();
animal = new Cat();
animal.Speak();
}medium
Solution
Step 1: Understand interface usage
The variable animal is of type IAnimal and first assigned a Dog object, so calling Speak() prints "Woof".Step 2: Change object and call method again
animal is then assigned a Cat object, so calling Speak() prints "Meow".Final Answer:
Woof Meow -> Option BQuick Check:
Interface variable calls method of assigned object [OK]
Hint: Interface variable calls method of current object type [OK]
Common Mistakes:
- Assuming interface variable fixes method output
- Mixing order of outputs
- Forgetting to implement interface methods
4. Identify the error in this code snippet:
interface IVehicle { void Drive(); }
class Car : IVehicle { }medium
Solution
Step 1: Check interface implementation rules
A class implementing an interface must provide bodies for all interface methods.Step 2: Analyze given code
Car class implements IVehicle but does not define Drive(), causing a compile error.Final Answer:
Car class must implement Drive() method -> Option DQuick Check:
Implement all interface methods [OK]
Hint: Implement all interface methods in the class [OK]
Common Mistakes:
- Thinking interface methods have bodies
- Assuming empty class is valid implementation
- Confusing abstract class requirement
5. You want to write a method that accepts any object that can be saved to a database. Which approach best uses interfaces to achieve this?
hard
Solution
Step 1: Understand interface benefits
Interfaces allow different classes to share a common method signature, enabling polymorphism.Step 2: Analyze options for flexibility and maintainability
Define an interface ISaveable with method Save(), then accept ISaveable parameter uses an interface ISaveable with Save() method, allowing any class implementing it to be passed in.Step 3: Compare other options
Use object type parameter and check type inside method uses object and type checks, which is less clean. Create a base class Saveable and inherit from it uses inheritance, which is less flexible. Write separate methods for each class type duplicates code.Final Answer:
Define an interface ISaveable with method Save(), then accept ISaveable parameter -> Option AQuick Check:
Interfaces enable flexible method parameters [OK]
Hint: Use interface parameters for flexible method inputs [OK]
Common Mistakes:
- Using object and type checks instead of interfaces
- Relying only on inheritance
- Writing duplicate methods for each type
