Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Using Default Interface Methods in C#
📖 Scenario: Imagine you are building a simple system for different types of devices that can perform actions. You want to define a common interface for devices with a default way to describe themselves.
🎯 Goal: Create an interface with a default method, implement it in a class, and see how the default method works without needing to be overridden.
📋 What You'll Learn
Create an interface called IDevice with a method Start() and a default method Describe() that returns a string.
Create a class called Printer that implements IDevice and provides its own Start() method.
Use the default Describe() method from the interface without overriding it in the class.
Create an instance of Printer and call both Start() and Describe() methods, then print their outputs.
💡 Why This Matters
🌍 Real World
Default interface methods help add new features to interfaces in large software projects without forcing all existing classes to change.
💼 Career
Understanding default interface methods is important for maintaining and evolving C# codebases in professional software development.
Progress0 / 4 steps
1
Create the IDevice interface with Start() method
Create an interface called IDevice with a method signature void Start().
C Sharp (C#)
Hint
Use the interface keyword and declare void Start(); inside it.
2
Add a default method Describe() to IDevice
Add a default method called Describe() to the IDevice interface that returns the string "This is a device.".
C Sharp (C#)
Hint
Use a method with a body inside the interface to provide a default implementation.
3
Create Printer class implementing IDevice with Start()
Create a class called Printer that implements IDevice and provide an implementation for the Start() method that returns no value but prints "Printer started." to the console.
C Sharp (C#)
Hint
Use public class Printer : IDevice and implement the Start() method with a console print.
4
Create Printer instance and call Start() and Describe()
Create an instance of Printer called printer. Call printer.Start() and then print the result of printer.Describe() to the console.
C Sharp (C#)
Hint
Create the Printer object, call Start(), then print Describe() result.
Practice
(1/5)
1. What is the main purpose of default interface methods in C#?
easy
A. Allow interfaces to have method bodies with default behavior
B. Force all implementing classes to override every method
C. Prevent interfaces from having any methods
D. Make interfaces behave like abstract classes
Solution
Step 1: Understand interface limitations before default methods
Interfaces could only declare methods without bodies, forcing all implementations to define them.
Step 2: Recognize the role of default interface methods
Default interface methods allow interfaces to provide a method body, so implementing classes can use or override it.
Final Answer:
Allow interfaces to have method bodies with default behavior -> Option A
Quick Check:
Default interface methods = method bodies in interfaces [OK]
Hint: Default interface methods add bodies to interfaces [OK]
Common Mistakes:
Thinking interfaces cannot have any method bodies
Confusing default methods with abstract methods
Believing all methods must be overridden
2. Which of the following is the correct syntax to declare a default interface method in C#?
easy
A. abstract void Show() { Console.WriteLine("Hello"); }
B. void Show();
C. default void Show() { Console.WriteLine("Hello"); }
D. void Show() => Console.WriteLine("Hello");
Solution
Step 1: Recall default method syntax in interfaces
Default interface methods can have bodies using either block or expression-bodied syntax.