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 an interface in C#?
An interface in C# is a contract that defines a set of methods and properties without implementing them. Classes that implement the interface must provide the implementation for all its members.
Click to reveal answer
beginner
How do you declare that a class implements an interface in C#?
You use a colon (:) after the class name followed by the interface name. For example: <br><code>public class MyClass : IMyInterface { }</code>
Click to reveal answer
intermediate
Can a class implement multiple interfaces in C#? How?
Yes, a class can implement multiple interfaces by listing them separated by commas after the colon. For example:<br><code>public class MyClass : IFirst, ISecond { }</code>
Click to reveal answer
intermediate
What happens if a class does not implement all members of an interface it declares?
The C# compiler will give an error because the class must provide implementations for all interface members unless the class is declared abstract.
Click to reveal answer
advanced
Explain explicit interface implementation in C#.
Explicit interface implementation means implementing interface members so they can only be accessed through the interface, not the class instance directly. This is done by prefixing the member with the interface name, like:<br><code>void IMyInterface.Method() { }</code>
Click to reveal answer
How do you declare that a class implements an interface named IExample?
Apublic class MyClass : IExample { }
Bpublic interface MyClass : IExample { }
Cpublic class MyClass implements IExample { }
Dpublic class MyClass inherits IExample { }
✗ Incorrect
In C#, a class implements an interface using a colon (:), not 'implements' or 'inherits'.
What must a class do when it implements an interface?
AImplement none of the interface members
BImplement at least one interface member
CImplement all interface members
DOnly declare the interface name
✗ Incorrect
A class must implement all members of the interface unless it is abstract.
Can a class implement more than one interface?
AYes, but only if interfaces inherit each other
BYes, by separating interface names with commas
CNo, only one interface is allowed
DNo, interfaces cannot be implemented
✗ Incorrect
C# allows multiple interfaces to be implemented by listing them separated by commas.
What is explicit interface implementation used for?
ATo implement interface members with different names
BTo make interface members public
CTo inherit from multiple classes
DTo hide interface members from the class's public API
✗ Incorrect
Explicit implementation hides interface members so they are accessible only through the interface type.
If a class does not implement all interface members, what happens?
ACompilation error unless the class is abstract
BThe program runs but with warnings
CThe missing members are auto-implemented
DThe interface is ignored
✗ Incorrect
The compiler requires all interface members to be implemented unless the class is abstract.
Describe how to implement an interface in a C# class and what rules must be followed.
Think about the syntax and what the compiler expects.
You got /4 concepts.
Explain the difference between implicit and explicit interface implementation in C#.
Consider how you access the implemented methods.
You got /4 concepts.
Practice
(1/5)
1. What does it mean to implement an interface in C#?
easy
A. A class provides code for all methods declared in the interface.
B. An interface inherits from a class.
C. A class hides all methods of the interface.
D. An interface creates objects directly.
Solution
Step 1: Understand interface purpose
An interface declares methods without code, setting a contract.
Step 2: Implementing means coding methods
A class that implements the interface must write the code for all those methods.
Final Answer:
A class provides code for all methods declared in the interface. -> Option A
Hint: Implementing means writing all interface methods in the class [OK]
Common Mistakes:
Thinking interfaces can create objects
Believing interfaces inherit from classes
Assuming methods are hidden, not implemented
2. Which of the following is the correct syntax to implement an interface IMyInterface in a class MyClass?
easy
A. interface MyClass : IMyInterface { }
B. class MyClass : IMyInterface { }
C. class MyClass implements IMyInterface { }
D. class MyClass inherits IMyInterface { }
Solution
Step 1: Recall C# interface syntax
In C#, a class implements an interface using a colon (:), not 'implements' or 'inherits'.
Step 2: Check each option
class MyClass : IMyInterface { } uses correct syntax: class MyClass : IMyInterface { }. Others use wrong keywords or declare interface as class.
Final Answer:
class MyClass : IMyInterface { } -> Option B
Quick Check:
Use colon (:) to implement interface [OK]
Hint: Use ':' to implement interface, not 'implements' or 'inherits' [OK]
Common Mistakes:
Using 'implements' keyword like Java
Trying to inherit interface with 'inherits'
Declaring interface as a class
3. What will be the output of this code?
interface IGreet { void SayHello(); } class Person : IGreet { public void SayHello() { Console.WriteLine("Hi!"); } } var p = new Person(); p.SayHello();
medium
A. No output
B. SayHello
C. Hi!
D. Compile error
Solution
Step 1: Understand interface and class
The interface IGreet requires SayHello method. Person implements it by printing "Hi!".
Step 2: Trace code execution
Creating Person object and calling SayHello prints "Hi!" to console.
Final Answer:
Hi! -> Option C
Quick Check:
Implemented method runs and prints output [OK]
Hint: Implemented method runs exactly as coded in class [OK]