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

Explicit interface implementation 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 - Explicit interface implementation
Define Interface with Method
Implement Interface Explicitly in Class
Create Class Object
Call Method via Interface Reference
Execute Explicit Implementation
Output Result
This flow shows how a class explicitly implements an interface method, requiring calls through the interface reference to access it.
Execution Sample
C Sharp (C#)
using System;
interface IExample {
    void Show();
}
class Demo : IExample {
    void IExample.Show() { Console.WriteLine("Hello from explicit"); }
}
class Program {
    static void Main() {
        IExample obj = new Demo();
        obj.Show();
    }
}
This code defines an interface and a class that explicitly implements its method, then calls the method via the interface reference.
Execution Table
StepActionEvaluationResult
1Define interface IExample with method Show()Interface createdIExample with Show() method
2Define class Demo implementing IExample explicitlyExplicit method IExample.Show() implementedDemo class with explicit Show()
3Create object obj as IExample reference to new Demo()obj is IExample type pointing to Demo instanceobj holds Demo instance
4Call obj.Show()Calls explicit IExample.Show() implementationPrints: Hello from explicit
5End of executionNo more statementsProgram ends
💡 All steps executed; explicit interface method called via interface reference.
Variable Tracker
VariableStartAfter Step 3Final
objnullIExample reference to Demo instanceIExample reference to Demo instance
Key Moments - 2 Insights
Why can't we call Show() directly on the Demo class instance?
Because Show() is implemented explicitly, it is only accessible through an interface reference, not directly on the class instance (see execution_table step 4).
How do we access the explicit interface method?
We must use a variable typed as the interface (IExample) to call the method, as shown in step 3 and 4 of the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the type of 'obj' after step 3?
ADirect Demo class instance
BDemo class instance referenced as IExample
CNull
DInterface IExample without instance
💡 Hint
Check variable_tracker row for 'obj' after step 3.
At which step does the explicit interface method get called?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at execution_table action and result columns for method call.
If we tried calling Show() on a Demo instance directly (not via interface), what would happen?
AIt would cause a compile-time error
BIt would call a default Show() method
CIt would call the explicit method successfully
DIt would call the interface method implicitly
💡 Hint
Refer to key_moments about accessibility of explicit methods.
Concept Snapshot
Explicit interface implementation syntax:
void InterfaceName.MethodName() { /* code */ }

- Method is only accessible via interface reference.
- Class instance cannot call explicit methods directly.
- Useful to avoid name conflicts or hide interface methods.
Full Transcript
This example shows how to implement an interface method explicitly in C#. The interface IExample declares a method Show(). The class Demo implements this method explicitly using the syntax void IExample.Show(). This means the Show method is not accessible directly on Demo instances. Instead, you must use an interface reference to call it. We create an object obj typed as IExample but holding a Demo instance. Calling obj.Show() runs the explicit implementation and prints the message. This technique helps when you want to hide interface methods from the class's public API or resolve method name conflicts.

Practice

(1/5)
1. What is the main purpose of explicit interface implementation in C#?
easy
A. To separate methods with the same name from different interfaces
B. To make interface methods public by default
C. To allow interface methods to be called without casting
D. To override base class methods automatically

Solution

  1. Step 1: Understand interface method conflicts

    When a class implements multiple interfaces with methods of the same name, explicit implementation helps separate them.
  2. Step 2: Recognize explicit implementation behavior

    Explicit interface methods are only accessible through the interface, avoiding confusion and conflicts.
  3. Final Answer:

    To separate methods with the same name from different interfaces -> Option A
  4. Quick Check:

    Explicit interface implementation = separate same-named methods [OK]
Hint: Explicit means access only via interface, not class object [OK]
Common Mistakes:
  • Thinking explicit makes methods public on the class
  • Assuming explicit allows calling without casting
  • Confusing explicit with overriding base methods
2. Which of the following is the correct syntax for explicit interface implementation of method Show() from interface IExample in class Demo?
easy
A. public void Show() { }
B. public void IExample.Show() { }
C. void IExample.Show() { }
D. void Show() IExample { }

Solution

  1. Step 1: Recall explicit implementation syntax

    Explicit interface methods are implemented with the interface name before the method: void InterfaceName.MethodName().
  2. Step 2: Match correct syntax

    void IExample.Show() { } matches this pattern exactly: void IExample.Show() { }.
  3. Final Answer:

    void IExample.Show() { } -> Option C
  4. Quick Check:

    Explicit syntax = void Interface.Method() [OK]
Hint: Explicit methods use interface name before method [OK]
Common Mistakes:
  • Adding public modifier to explicit method
  • Placing interface name after method name
  • Using invalid syntax like 'void Show() IExample'
3. What will be the output of the following code?
interface IA { void Display(); }
interface IB { void Display(); }
class Test : IA, IB {
    void IA.Display() { Console.WriteLine("IA Display"); }
    void IB.Display() { Console.WriteLine("IB Display"); }
}

var obj = new Test();
// obj.Display(); // Line A
((IA)obj).Display();
((IB)obj).Display();
medium
A. Compilation error at Line A
B. IA Display\nIB Display
C. IB Display\nIA Display
D. Runtime error

Solution

  1. Step 1: Understand explicit interface calls

    Explicit interface methods cannot be called directly on the class object, so obj.Display() causes error if uncommented.
  2. Step 2: Check interface casts and calls

    Casting to IA calls IA.Display() printing "IA Display"; casting to IB calls IB.Display() printing "IB Display".
  3. Final Answer:

    IA Display IB Display -> Option B
  4. Quick Check:

    Explicit calls via interface = correct output [OK]
Hint: Call explicit methods only via interface cast [OK]
Common Mistakes:
  • Trying to call explicit method directly on class object
  • Mixing output order
  • Expecting runtime errors instead of compile errors
4. Identify the error in the following code snippet implementing explicit interface method:
interface IAlpha { void Run(); }
class Beta : IAlpha {
    public void IAlpha.Run() {
        Console.WriteLine("Running");
    }
}
medium
A. Explicit interface method cannot have public modifier
B. Method name must be different from interface
C. Interface name should not be used in method implementation
D. Missing override keyword

Solution

  1. Step 1: Check explicit implementation rules

    Explicit interface methods must not have access modifiers like public; they are implicitly private.
  2. Step 2: Identify error in code

    The code uses public void IAlpha.Run(), which is invalid syntax for explicit implementation.
  3. Final Answer:

    Explicit interface method cannot have public modifier -> Option A
  4. Quick Check:

    Explicit methods = no public keyword [OK]
Hint: Remove public from explicit interface methods [OK]
Common Mistakes:
  • Adding public modifier to explicit methods
  • Confusing explicit with normal method override
  • Forgetting interface name in method signature
5. Given two interfaces IX and IY both having method Process(), how can a class Worker implement both explicitly and allow calling Process() without casting?
hard
A. Use inheritance instead of interfaces
B. Implement explicit methods only, no public method needed
C. Implement only one interface explicitly and the other implicitly
D. Implement explicit methods and add a public method calling one interface method

Solution

  1. Step 1: Understand explicit implementation limits

    Explicit interface methods are accessible only via interface references, not directly on class objects.
  2. Step 2: Provide public method to call explicit method

    To call Process() without casting, class must have a public method that internally calls one explicit interface method.
  3. Final Answer:

    Implement explicit methods and add a public method calling one interface method -> Option D
  4. Quick Check:

    Public wrapper calls explicit method = direct access [OK]
Hint: Add public method calling explicit interface method [OK]
Common Mistakes:
  • Expecting explicit methods callable without casting
  • Implementing only one interface explicitly
  • Ignoring need for public wrapper method