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

Default interface methods in C Sharp (C#) - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a default method in the interface.

C Sharp (C#)
public interface IExample {
    void Show();
    [1] DefaultMethod() {
        Console.WriteLine("Default implementation");
    }
}
Drag options to blanks, or click blank then click option'
Adefault
Bpublic
Cstatic
Dvoid
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'default' keyword before method name.
Trying to make the method static inside interface.
2fill in blank
medium

Complete the code to call the default interface method from a class implementing the interface.

C Sharp (C#)
public class MyClass : IExample {
    public void Show() {
        Console.WriteLine("Show method");
    }
    public void CallDefault() {
        [1].DefaultMethod();
    }
}
Drag options to blanks, or click blank then click option'
AIExample
Bbase
Cthis
Ddefault
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'this' or 'base' to call the interface method.
Trying to call the method without specifying the interface.
3fill in blank
hard

Fix the error in the interface default method declaration.

C Sharp (C#)
public interface ICalc {
    [1] int Add(int a, int b) {
        return a + b;
    }
}
Drag options to blanks, or click blank then click option'
Apublic
Bvoid
Cstatic
Ddefault
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'default' keyword incorrectly.
Declaring the method as 'static' inside interface.
4fill in blank
hard

Fill both blanks to create a default method with a return value and call it from the class.

C Sharp (C#)
public interface IPrinter {
    [1] string GetMessage() {
        return "Hello from interface";
    }
}

public class Printer : IPrinter {
    public string GetMessage() {
        return [2].GetMessage();
    }
}
Drag options to blanks, or click blank then click option'
Apublic
BIPrinter
Cdefault
Dthis
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'default' keyword in method declaration.
Using 'this' instead of interface name to call the method.
5fill in blank
hard

Fill all three blanks to override a default interface method in a class and call both versions.

C Sharp (C#)
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);
    }
}
Drag options to blanks, or click blank then click option'
Apublic
BILogger
CLog
Ddefault
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'default' keyword in method declaration.
Trying to call the default method without interface name.