Default interface methods in C Sharp (C#) - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When using default interface methods in C#, it's important to understand how the program's running time changes as input grows.
We want to see how calling these methods affects the total work done by the program.
Analyze the time complexity of the following code snippet.
interface IPrinter
{
void Print();
void PrintMultiple(int times)
{
for (int i = 0; i < times; i++)
Print();
}
}
class ConsolePrinter : IPrinter
{
public void Print() => Console.WriteLine("Hello");
}
// Usage
IPrinter printer = new ConsolePrinter();
printer.PrintMultiple(5);
This code defines an interface with a default method that calls another method multiple times. The class implements the interface and the default method is used to print a message repeatedly.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop inside the default method
PrintMultiplethat callsPrintrepeatedly. - How many times: The loop runs
timestimes, which depends on the input parameter.
Explain the growth pattern intuitively.
| Input Size (times) | Approx. Operations (Print calls) |
|---|---|
| 10 | 10 |
| 100 | 100 |
| 1000 | 1000 |
Pattern observation: The number of print calls grows directly with the input number times. If you double times, the work doubles.
Time Complexity: O(n)
This means the time to run PrintMultiple grows linearly with the number of times you want to print.
[X] Wrong: "Default interface methods run instantly and don't add to execution time."
[OK] Correct: Even though the method is defined in the interface, it still runs its code each time it's called, so the loop inside causes repeated work proportional to the input.
Understanding how default interface methods behave helps you reason about code performance and design choices, a useful skill in real projects and interviews.
"What if the default method called another default method inside the interface that also loops? How would the time complexity change?"
Practice
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 AQuick Check:
Default interface methods = method bodies in interfaces [OK]
- Thinking interfaces cannot have any method bodies
- Confusing default methods with abstract methods
- Believing all methods must be overridden
Solution
Step 1: Recall default method syntax in interfaces
Default interface methods can have bodies using either block or expression-bodied syntax.Step 2: Identify correct syntax among options
void Show() => Console.WriteLine("Hello"); uses expression-bodied syntax correctly inside interface method declaration.Final Answer:
void Show() => Console.WriteLine("Hello"); -> Option DQuick Check:
Default method syntax = method with body in interface [OK]
- Using 'default' keyword before method
- Omitting method body
- Writing method body without braces or expression syntax
interface IExample { void Show() => Console.WriteLine("Default"); }
class Test : IExample { }
var t = new Test();
t.Show();Solution
Step 1: Check if class implements Show()
Class Test does not implement Show(), but interface provides default implementation.Step 2: Understand default method usage
Since Test inherits IExample, it uses the default Show() method from interface.Final Answer:
Default -> Option CQuick Check:
Default method runs if not overridden [OK]
- Assuming missing implementation causes error
- Expecting runtime error without override
- Thinking interface methods can't have bodies
interface ICalc { int Add(int a, int b) => a + b; }
class Calc : ICalc { public int Add(int a, int b); }Solution
Step 1: Check Calc class Add method declaration
Calc declares Add with a semicolon but no body, which is invalid in a class.Step 2: Understand method implementation requirements
Class methods must have bodies unless abstract; here Add is not abstract, so body is required.Final Answer:
Missing method body in Calc's Add method -> Option AQuick Check:
Class methods need bodies unless abstract [OK]
- Thinking interface can't have bodies
- Forgetting to add method body in class
- Assuming method should be static
interface ILogger { void Log(string msg) => Console.WriteLine($"Log: {msg}"); }
class FileLogger : ILogger { public void Log(string msg) => Console.WriteLine($"File: {msg}"); }
class ConsoleLogger : ILogger { }What will be the output of:
ILogger logger1 = new FileLogger();
ILogger logger2 = new ConsoleLogger();
logger1.Log("Test1");
logger2.Log("Test2");
Solution
Step 1: Analyze FileLogger's Log method
FileLogger overrides Log to print "File: {msg}".Step 2: Analyze ConsoleLogger's Log method
ConsoleLogger does not override Log, so uses interface default "Log: {msg}".Step 3: Predict output for each logger
logger1.Log("Test1") prints "File: Test1"; logger2.Log("Test2") prints "Log: Test2".Final Answer:
File: Test1 Log: Test2 -> Option BQuick Check:
Override changes output; default runs if no override [OK]
- Assuming both use default method
- Expecting compilation error due to default method
- Confusing which method runs
