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 or structs that implement the interface must provide the implementation.
Click to reveal answer
beginner
How do you declare an interface in C#?
Use the interface keyword followed by the interface name and a block containing method/property signatures. For example:
public interface IExample { void DoWork(); }
Click to reveal answer
intermediate
Can interfaces contain fields or constructors in C#?
No, interfaces cannot contain fields or constructors. They only declare method, property, event, or indexer signatures without implementation.
Click to reveal answer
intermediate
What access modifier is used for interface members in C#?
Interface members are implicitly public and cannot have access modifiers. All members declared in an interface are public by default.
Click to reveal answer
beginner
How does a class implement an interface in C#?
A class implements an interface by using a colon <code>:</code> followed by the interface name, then providing implementations for all interface members. Example:<br><pre>public class Worker : IExample { public void DoWork() { /* code */ } }</pre>
Click to reveal answer
Which keyword is used to declare an interface in C#?
Ainterface
Bclass
Cstruct
Dimplements
✗ Incorrect
The interface keyword is used to declare an interface in C#.
Can an interface in C# contain method implementations?
AYes, always
BOnly default interface methods (C# 8.0+)
COnly private methods
DNo, never
✗ Incorrect
Starting with C# 8.0, interfaces can have default implementations for methods, but traditionally interfaces only declare signatures.
What access modifier do interface members have by default?
Apublic
Bprotected
Cprivate
Dinternal
✗ Incorrect
Interface members are implicitly public and cannot have other access modifiers.
How does a class indicate it implements an interface?
AUsing the keyword 'inherits'
BUsing the keyword 'implements'
CUsing a colon ':' followed by the interface name
DUsing angle brackets '<>'
✗ Incorrect
In C#, a class implements an interface by using a colon ':' followed by the interface name.
Which of these can NOT be declared inside an interface?
AEvents
BProperties
CMethod signatures
DFields
✗ Incorrect
Interfaces cannot contain fields; they only declare signatures for methods, properties, events, and indexers.
Explain how to declare an interface in C# and how a class implements it.
Think about the contract nature of interfaces and how classes fulfill that contract.
You got /4 concepts.
What are the limitations of interfaces in C# regarding members they can contain?
Consider what an interface represents and what it cannot do.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of an interface in C#?
easy
A. To define a contract with method and property signatures only
B. To implement all method bodies for a class
C. To store data like variables and constants
D. To create an instance of a class directly
Solution
Step 1: Understand what an interface is
An interface only declares method and property signatures without implementations.
Step 2: Compare with other options
Interfaces do not implement methods or store data; they define a contract for classes.
Final Answer:
To define a contract with method and property signatures only -> Option A
Quick Check:
Interface purpose = contract definition [OK]
Hint: Interfaces declare methods, they don't implement them [OK]
Common Mistakes:
Thinking interfaces contain method bodies
Confusing interfaces with classes
Believing interfaces store data
2. Which of the following is the correct syntax to declare an interface named IMyInterface in C#?
easy
A. interface IMyInterface { void MyMethod(); }
B. class IMyInterface { void MyMethod(); }
C. interface IMyInterface() { void MyMethod(); }
D. interface IMyInterface[] { void MyMethod(); }
Solution
Step 1: Check the keyword and name format
Interfaces use the keyword interface followed by the name without parentheses or brackets.
Hint: Use 'interface Name { }' without parentheses or brackets [OK]
Common Mistakes:
Using class keyword instead of interface
Adding parentheses after interface name
Using brackets [] after interface name
3. What will be the output of the following code?
interface IExample { void Show(); }
class Demo : IExample {
public void Show() { Console.WriteLine("Hello Interface"); }
}
class Program {
static void Main() {
IExample obj = new Demo();
obj.Show();
}
}
medium
A. Compilation error: Show method missing
B. Hello Interface
C. Runtime error: Cannot create interface instance
D. No output
Solution
Step 1: Understand interface implementation
The class Demo implements IExample and provides the Show method.
Step 2: Analyze the Main method
An object of Demo is created and assigned to an IExample reference, then Show() is called, printing the message.
Final Answer:
Hello Interface -> Option B
Quick Check:
Interface method call prints message [OK]
Hint: Interface methods must be implemented to avoid errors [OK]
Common Mistakes:
Assuming interfaces can be instantiated directly
Forgetting to implement interface methods
Expecting no output without method body
4. Identify the error in the following interface declaration:
interface ITest {
void Run() {}
}
medium
A. Missing semicolon after method declaration
B. Method name must be lowercase
C. Interface name must start with lowercase 'i'
D. Interfaces cannot have method bodies
Solution
Step 1: Check method declaration in interface
Interfaces only declare method signatures without bodies (no curly braces).
Step 2: Validate other syntax rules
Method names can be any case; interface names usually start with uppercase 'I'. Semicolon is required after signature.
Final Answer:
Interfaces cannot have method bodies -> Option D
Quick Check:
Interface methods = signatures only [OK]
Hint: No method bodies allowed inside interfaces [OK]
Common Mistakes:
Adding method bodies inside interfaces
Confusing naming conventions with syntax errors
Omitting semicolon after method signature
5. You want to declare an interface IVehicle with two methods: Start() and Stop(). Which of the following is the correct way to declare it and implement it in a class Car?
hard
A. interface IVehicle { void Start(); void Stop(); }
class Car { public void Start() { } public void Stop() { } }
B. interface IVehicle { void Start() {} void Stop() {} }
class Car : IVehicle { }
C. interface IVehicle { void Start(); void Stop(); }
class Car : IVehicle { public void Start() { Console.WriteLine("Car started"); } public void Stop() { Console.WriteLine("Car stopped"); } }
D. interface IVehicle { void Start(); void Stop(); }
class Car : IVehicle { void Start() { } void Stop() { } }
Solution
Step 1: Declare interface with method signatures only
IVehicle must declare Start() and Stop() without bodies.
Step 2: Implement interface methods publicly in class
Car must implement both methods with public access and provide method bodies.
Step 3: Check other options for errors
interface IVehicle { void Start() {} void Stop() {} }
class Car : IVehicle { } has method bodies in interface (invalid). interface IVehicle { void Start(); void Stop(); }
class Car { public void Start() { } public void Stop() { } } does not implement interface. interface IVehicle { void Start(); void Stop(); }
class Car : IVehicle { void Start() { } void Stop() { } } implements methods but lacks public modifier, causing error.
Final Answer:
Correct interface and class implementation with public methods -> Option C
Quick Check:
Interface methods declared; class implements publicly [OK]
Hint: Interface methods need public implementation in classes [OK]