Complete the code to declare a default method in the interface.
public interface IExample {
void Show();
[1] DefaultMethod() {
Console.WriteLine("Default implementation");
}
}The method in an interface with a default implementation must have an access modifier like public. The keyword default is not used here.
Complete the code to call the default interface method from a class implementing the interface.
public class MyClass : IExample { public void Show() { Console.WriteLine("Show method"); } public void CallDefault() { [1].DefaultMethod(); } }
To call a default interface method explicitly, you use the interface name followed by the method.
Fix the error in the interface default method declaration.
public interface ICalc {
[1] int Add(int a, int b) {
return a + b;
}
}Interface methods with default implementations must be declared with an access modifier like public. The default keyword is not used here.
Fill both blanks to create a default method with a return value and call it from the class.
public interface IPrinter {
[1] string GetMessage() {
return "Hello from interface";
}
}
public class Printer : IPrinter {
public string GetMessage() {
return [2].GetMessage();
}
}The default method must be declared public. To call it from the class, use the interface name.
Fill all three blanks to override a default interface method in a class and call both versions.
public interface ILogger {
[1] void Log(string message) {
Console.WriteLine($"Interface log: {message}");
}
}
public class Logger : ILogger {
public void Log(string message) {
Console.WriteLine($"Class log: {message}");
[2].[3](message);
}
}The interface method must be declared public. To call the default method from the class, use the interface name and method name.