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

Default interface methods in C Sharp (C#) - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What are default interface methods in C#?
Default interface methods allow interfaces to provide a default implementation for methods. This means classes implementing the interface don't have to implement those methods unless they want to override the default behavior.
Click to reveal answer
beginner
How do you declare a default method in an interface?
Inside the interface, you define the method with a body using curly braces. For example:<br>
interface IExample { void Show() { Console.WriteLine("Hello"); } }
Click to reveal answer
beginner
Can a class override a default interface method?
Yes, a class implementing the interface can provide its own implementation of the default method, which will replace the default one.
Click to reveal answer
intermediate
Why were default interface methods introduced in C#?
They were introduced to allow adding new methods to interfaces without breaking existing implementations. This helps evolve interfaces over time.
Click to reveal answer
intermediate
What happens if a class implements two interfaces with the same default method signature?
The class must explicitly implement the method to resolve the conflict, otherwise the compiler will give an error.
Click to reveal answer
What is the main benefit of default interface methods?
AAllow interfaces to have method implementations
BMake interfaces abstract classes
CPrevent classes from implementing interfaces
DRemove the need for interfaces
How do you provide a default implementation in an interface method?
ABy declaring the method without a body
BBy declaring the method with a body inside the interface
CBy implementing the method in the class only
DBy using abstract keyword
If a class implements an interface with a default method, what happens if the class does not implement that method?
ARuntime exception
BCompiler error
CMethod is ignored
DClass uses the default method implementation
Can default interface methods access private members of the implementing class?
AYes, always
BOnly if marked internal
CNo, they cannot access private members
DOnly if class is sealed
What must a class do if it implements two interfaces with conflicting default methods?
AExplicitly implement the conflicting method
BNothing, compiler resolves automatically
CRemove one interface
DMark class as abstract
Explain what default interface methods are and why they are useful.
Think about how interfaces used to work before default methods.
You got /3 concepts.
    Describe how a class can override a default interface method and what happens if it does not.
    Consider what happens when you want to change the default behavior.
    You got /3 concepts.