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
Recall & Review
beginner
What is explicit interface implementation in C#?
It is a way to implement interface members so they are only accessible through the interface, not through the class instance directly.
Click to reveal answer
intermediate
Why use explicit interface implementation?
To avoid name conflicts when multiple interfaces have members with the same name, or to hide interface methods from the class's public API.
Click to reveal answer
beginner
How do you declare an explicit interface implementation in C#?
Use the interface name followed by a dot before the member name, for example: void IInterface.Method().
Click to reveal answer
beginner
Can you call an explicitly implemented interface method directly from a class instance?
No, you must cast the class instance to the interface type first to call the method.
Click to reveal answer
intermediate
What happens if two interfaces have methods with the same signature and you implement them explicitly?
You can provide separate implementations for each interface method, avoiding conflicts and clarifying which method belongs to which interface.
Click to reveal answer
How do you call an explicitly implemented interface method from a class instance obj?
Aobj->Method()
Bobj.Method()
C((InterfaceType)obj).Method()
DInterfaceType.Method(obj)
✗ Incorrect
Explicit interface methods are only accessible through the interface, so you must cast the object to the interface type first.
Which syntax correctly declares an explicit interface implementation for method Show() in interface IDisplay?
Avoid IDisplay.Show()
Bpublic void Show()
CIDisplay.Show()
Dpublic IDisplay.Show()
✗ Incorrect
Explicit interface implementation uses the interface name followed by a dot before the method name without an access modifier.
What is a main benefit of explicit interface implementation?
AIt allows hiding interface methods from the class's public API
BIt automatically implements all interface methods
CIt makes interface methods public by default
DIt allows interface methods to be static
✗ Incorrect
Explicit implementation hides the interface methods from the class's public interface, making them accessible only through the interface.
If a class implements two interfaces with the same method name, how can explicit implementation help?
ABy merging both methods into one
BBy providing separate implementations for each interface method
CBy ignoring one interface method
DBy renaming the class
✗ Incorrect
Explicit implementation allows separate method bodies for each interface method, avoiding conflicts.
Which statement is true about explicit interface implementation?
AExplicit interface methods can be called without casting
BExplicit interface methods are inherited by derived classes
CExplicit interface methods cannot be private
DExplicit interface methods do not have access modifiers
✗ Incorrect
Explicit interface implementations do not have access modifiers like public or private; they are implicitly private and accessible only via the interface.
Explain how explicit interface implementation works and why you might use it.
Think about how to keep interface methods separate from class methods.
You got /4 concepts.
Describe how to call an explicitly implemented interface method from an object instance.
Remember explicit methods are hidden from the class itself.
You got /3 concepts.
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
Step 1: Understand interface method conflicts
When a class implements multiple interfaces with methods of the same name, explicit implementation helps separate them.
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
Step 1: Understand explicit interface calls
Explicit interface methods cannot be called directly on the class object, so obj.Display() causes error if uncommented.
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".
Final Answer:
IA Display
IB Display -> Option B
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
Step 1: Check explicit implementation rules
Explicit interface methods must not have access modifiers like public; they are implicitly private.
Step 2: Identify error in code
The code uses public void IAlpha.Run(), which is invalid syntax for explicit implementation.
Final Answer:
Explicit interface method cannot have public modifier -> Option A
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
Step 1: Understand explicit implementation limits
Explicit interface methods are accessible only via interface references, not directly on class objects.
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.
Final Answer:
Implement explicit methods and add a public method calling one interface method -> Option D
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