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

Explicit interface implementation 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 explicitly implement the interface method.

C Sharp (C#)
interface IExample {
    void Show();
}

class Demo : IExample {
    void IExample.[1]() {
        Console.WriteLine("Explicit implementation");
    }
}
Drag options to blanks, or click blank then click option'
AShow
BDisplay
CShowMessage
DPrint
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different method name than the interface method.
Forgetting to prefix the method with the interface name.
2fill in blank
medium

Complete the code to call the explicitly implemented interface method from an interface reference.

C Sharp (C#)
interface IExample {
    void Show();
}

class Demo : IExample {
    void IExample.Show() {
        Console.WriteLine("Explicit implementation");
    }
}

class Program {
    static void Main() {
        Demo obj = new Demo();
        IExample iobj = obj;
        iobj.[1]();
    }
}
Drag options to blanks, or click blank then click option'
AShow
BDisplay
CPrint
DShowMessage
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to call the method directly from the class instance.
Using a wrong method name.
3fill in blank
hard

Fix the error in the explicit interface implementation method signature.

C Sharp (C#)
interface IExample {
    void Show();
}

class Demo : IExample {
    void IExample.[1]() {
        Console.WriteLine("Explicit implementation");
    }
}
Drag options to blanks, or click blank then click option'
AShowMessage
BDisplay
CPrint
DShow
Attempts:
3 left
💡 Hint
Common Mistakes
Adding 'public' or other access modifiers to explicit interface methods.
Using a different method name than the interface.
4fill in blank
hard

Fill both blanks to explicitly implement two interface methods correctly.

C Sharp (C#)
interface IFirst {
    void MethodA();
}
interface ISecond {
    void MethodB();
}

class Demo : IFirst, ISecond {
    void IFirst.[1]() {
        Console.WriteLine("First method");
    }
    void ISecond.[2]() {
        Console.WriteLine("Second method");
    }
}
Drag options to blanks, or click blank then click option'
AMethodA
BMethodC
CMethodB
DMethodD
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up method names between interfaces.
Using incorrect method names.
5fill in blank
hard

Fill all three blanks to explicitly implement an interface and call its method correctly.

C Sharp (C#)
interface IExample {
    void Show();
}

class Demo : IExample {
    void IExample.[1]() {
        Console.WriteLine("Explicit implementation");
    }
}

class Program {
    static void Main() {
        Demo obj = new Demo();
        IExample iobj = [2];
        iobj.[3]();
    }
}
Drag options to blanks, or click blank then click option'
AShow
Bobj
Ddemo
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable names for the interface reference.
Calling the method directly from the class instance instead of the interface reference.