Interface as contract mental model in C Sharp (C#) - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When we use interfaces as contracts in C#, we want to know how the program's speed changes as we add more objects that follow the interface.
We ask: How does the time to run methods grow when many objects implement the same interface?
Analyze the time complexity of the following code snippet.
interface IWorker
{
void Work();
}
class Worker : IWorker
{
public void Work() { /* do some work */ }
}
void ProcessWorkers(List workers)
{
foreach (var worker in workers)
worker.Work();
}
This code calls the Work method on each object in a list that implements the IWorker interface.
- Primary operation: Calling the Work() method on each worker object.
- How many times: Once for each worker in the list.
As the number of workers grows, the total calls to Work() grow the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 calls to Work() |
| 100 | 100 calls to Work() |
| 1000 | 1000 calls to Work() |
Pattern observation: The number of operations grows directly with the number of workers.
Time Complexity: O(n)
This means the time to process all workers grows in a straight line as you add more workers.
[X] Wrong: "Using an interface makes the code slower because of extra overhead."
[OK] Correct: Calling methods through an interface adds very little overhead, and the main time cost comes from how many times you call the method, not the interface itself.
Understanding how interfaces affect time helps you explain how your code scales when many objects follow the same contract.
"What if the Work() method itself contains a loop over a large list? How would that change the time complexity?"
Practice
What does an interface in C# represent?
Solution
Step 1: Understand the role of an interface
An interface defines a set of method signatures without implementations.Step 2: Compare with classes
Classes implement interfaces by providing method bodies, fulfilling the contract.Final Answer:
A contract that defines methods a class must implement -> Option AQuick Check:
Interface = contract for methods [OK]
- Thinking interfaces contain method code
- Confusing interfaces with classes
- Believing interfaces store data
Which of the following is the correct way to declare an interface in C#?
?
Solution
Step 1: Check interface declaration syntax
Interfaces use the keyword 'interface' followed by the name and method signatures without bodies.Step 2: Identify correct method signature
Method declarations in interfaces do not have bodies, so no curly braces after method.Final Answer:
interface IAnimal { void Speak(); } -> Option AQuick Check:
Interface syntax = keyword + method signatures [OK]
- Adding parentheses after interface name
- Using class keyword instead of interface
- Providing method bodies inside interface
What will be the output of the following code?
interface IWorker { void Work(); }
class Employee : IWorker {
public void Work() { Console.WriteLine("Employee working"); }
}
class Robot : IWorker {
public void Work() { Console.WriteLine("Robot working"); }
}
class Program {
static void Main() {
IWorker w = new Robot();
w.Work();
}
}Solution
Step 1: Identify the object type assigned to interface variable
The variable 'w' is of type IWorker but assigned a new Robot instance.Step 2: Determine which Work() method runs
Calling w.Work() runs Robot's Work method, printing "Robot working".Final Answer:
Robot working -> Option DQuick Check:
Interface variable calls actual object's method [OK]
- Assuming interface variable calls Employee method
- Expecting compilation error due to interface
- Thinking no output will print
Identify the error in this code snippet:
interface IShape {
double Area();
}
class Circle : IShape {
public double Area() {
return 3.14 * radius * radius;
}
}Solution
Step 1: Check Circle class members
The method Area uses 'radius' but no radius variable or property is declared in Circle.Step 2: Understand interface method return type
Interface method returning double is valid; no error there.Final Answer:
Missing radius field or property in Circle class -> Option BQuick Check:
Undefined variable 'radius' causes error [OK]
- Thinking interface methods can't return values
- Believing class can't implement interface
- Assuming method return type must be void
You want to create a system where different devices can Start() and Stop() but each device does it differently. How should you use interfaces to design this?
Solution
Step 1: Understand interface purpose
Interfaces define a contract for methods without implementation, perfect for different device behaviors.Step 2: Apply interface to devices
Define IDevice with Start and Stop, then each device class implements these methods with its own details.Final Answer:
Define an interface IDevice with Start and Stop methods, then implement it in each device class -> Option CQuick Check:
Interface = shared method rules, different implementations [OK]
- Using base class limits flexibility
- Skipping interface loses contract benefits
- Confusing abstract classes with interfaces
