0
0
C Sharp (C#)programming~10 mins

Explicit interface implementation in C Sharp (C#) - Step-by-Step Execution

Choose your learning style9 modes available
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.