Bird
Raised Fist0
C Sharp (C#)programming~10 mins

Default interface methods in C Sharp (C#) - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

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
Concept Flow - Default interface methods
Define interface with default method
Implement interface in class
Create instance of class
Call default method via interface
Execute default method code
Output result
The flow shows how an interface with a default method is defined, implemented by a class, and how calling the method runs the default code if not overridden.
Execution Sample
C Sharp (C#)
interface IGreeter {
    void Greet() {
        Console.WriteLine("Hello from default method!");
    }
}

class Greeter : IGreeter {}

IGreeter g = new Greeter();
g.Greet();
This code defines an interface with a default method, implements it in a class without overriding, then calls the default method.
Execution Table
StepActionEvaluationResult
1Define interface IGreeter with default method Greet()Interface created with default methodIGreeter has Greet() with default implementation
2Define class Greeter implementing IGreeterClass created without overriding Greet()Greeter inherits default Greet()
3Create instance g of Greeter as IGreeterInstance createdg is Greeter instance
4Call g.Greet()No override found in GreeterDefault Greet() runs
5Execute default Greet() methodConsole.WriteLine runsOutput: Hello from default method!
💡 Method call completes after default method executes and prints output
Variable Tracker
VariableStartAfter Step 3After Step 4Final
gnullGreeter instanceGreeter instanceGreeter instance
Key Moments - 2 Insights
Why does calling Greet() on Greeter instance run the default method?
Because Greeter does not override Greet(), the call uses the interface's default implementation as shown in execution_table step 4.
Can the class override the default interface method?
Yes, if the class defines its own Greet() method, it will run that instead of the default, replacing the behavior in step 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output when g.Greet() is called?
ACompilation error
B"Hello from default method!"
CNo output
D"Hello from Greeter!"
💡 Hint
See execution_table row 5 where the default method prints the output
At which step does the program create the instance of Greeter?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Check execution_table row 3 for instance creation
If Greeter class had its own Greet() method, what would change in the execution?
ACompilation error occurs
BThe default method would still run
CThe class method would run instead of default
DInterface method is removed
💡 Hint
Refer to key_moments about overriding default methods
Concept Snapshot
Default interface methods allow interfaces to provide method bodies.
Classes implementing the interface can use these defaults without overriding.
If a class overrides, its method runs instead.
This helps add new methods to interfaces without breaking existing code.
Call the method via interface reference to use default implementation.
Full Transcript
This example shows how default interface methods work in C#. First, an interface IGreeter is defined with a default method Greet() that prints a message. Then, a class Greeter implements IGreeter but does not override Greet(). When we create an instance of Greeter and call Greet() via the interface reference, the default method runs and prints the message. The execution table traces each step: defining interface and class, creating instance, calling method, and output. The variable tracker shows the instance variable g changes from null to a Greeter object. Key moments clarify why the default method runs and how overriding works. The quiz tests understanding of output, instance creation step, and effect of overriding. The snapshot summarizes the concept simply for quick review.

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

  1. Step 1: Understand interface limitations before default methods

    Interfaces could only declare methods without bodies, forcing all implementations to define them.
  2. 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.
  3. Final Answer:

    Allow interfaces to have method bodies with default behavior -> Option A
  4. 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

  1. Step 1: Recall default method syntax in interfaces

    Default interface methods can have bodies using either block or expression-bodied syntax.
  2. Step 2: Identify correct syntax among options

    void Show() => Console.WriteLine("Hello"); uses expression-bodied syntax correctly inside interface method declaration.
  3. Final Answer:

    void Show() => Console.WriteLine("Hello"); -> Option D
  4. Quick Check:

    Default method syntax = method with body in interface [OK]
Hint: Default methods have bodies, unlike abstract declarations [OK]
Common Mistakes:
  • Using 'default' keyword before method
  • Omitting method body
  • Writing method body without braces or expression syntax
3. What will be the output of this code?
interface IExample { void Show() => Console.WriteLine("Default"); }
class Test : IExample { }
var t = new Test();
t.Show();
medium
A. Runtime error
B. Compilation error: Show() not implemented
C. Default
D. No output

Solution

  1. Step 1: Check if class implements Show()

    Class Test does not implement Show(), but interface provides default implementation.
  2. Step 2: Understand default method usage

    Since Test inherits IExample, it uses the default Show() method from interface.
  3. Final Answer:

    Default -> Option C
  4. Quick Check:

    Default method runs if not overridden [OK]
Hint: If not overridden, default interface method runs [OK]
Common Mistakes:
  • Assuming missing implementation causes error
  • Expecting runtime error without override
  • Thinking interface methods can't have bodies
4. Identify the error in this code snippet:
interface ICalc { int Add(int a, int b) => a + b; }
class Calc : ICalc { public int Add(int a, int b); }
medium
A. Missing method body in Calc's Add method
B. Interface cannot have method bodies
C. Calc class must be abstract
D. Add method should be static

Solution

  1. Step 1: Check Calc class Add method declaration

    Calc declares Add with a semicolon but no body, which is invalid in a class.
  2. Step 2: Understand method implementation requirements

    Class methods must have bodies unless abstract; here Add is not abstract, so body is required.
  3. Final Answer:

    Missing method body in Calc's Add method -> Option A
  4. Quick Check:

    Class methods need bodies unless abstract [OK]
Hint: Class methods need bodies unless marked abstract [OK]
Common Mistakes:
  • Thinking interface can't have bodies
  • Forgetting to add method body in class
  • Assuming method should be static
5. Given this interface and classes:
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");
hard
A. File: Test1 File: Test2
B. File: Test1 Log: Test2
C. Log: Test1 Log: Test2
D. Compilation error

Solution

  1. Step 1: Analyze FileLogger's Log method

    FileLogger overrides Log to print "File: {msg}".
  2. Step 2: Analyze ConsoleLogger's Log method

    ConsoleLogger does not override Log, so uses interface default "Log: {msg}".
  3. Step 3: Predict output for each logger

    logger1.Log("Test1") prints "File: Test1"; logger2.Log("Test2") prints "Log: Test2".
  4. Final Answer:

    File: Test1 Log: Test2 -> Option B
  5. Quick Check:

    Override changes output; default runs if no override [OK]
Hint: Override changes output; default runs if no override [OK]
Common Mistakes:
  • Assuming both use default method
  • Expecting compilation error due to default method
  • Confusing which method runs