C#.NET — Choose the correct implementation for the interface: interface IMyInterface { double MyFun(Single i); }
-
Aclass MyClass { double MyFun(Single i) as IMyInterface.MyFun { /* code / } }
-
Bclass MyClass { MyFun(Single i) As Double { / code / } }
-
Cclass MyClass: implements IMyInterface { double fun(Single si) implements IMyInterface.MyFun() { / code / } }
-
Dclass MyClass : IMyInterface { double IMyInterface.MyFun(Single i) { / code / } }
-
ENone of these
Answer
Correct Answer: class MyClass : IMyInterface { double IMyInterface.MyFun(Single i) { / code / } }
Explanation
Introduction / Context:Interfaces declare member signatures. Implementations may be provided either implicitly (public members matching the signature) or explicitly (qualified with the interface name). This item asks you to recognize valid C# syntax for explicit interface implementation.
Given Data / Assumptions:
- Interface: double MyFun(Single i).
- Language: C# (not VB).
Concept / Approach:Explicit interface implementation syntax is: returnType InterfaceName.MemberName(parameters) { ... } and appears within a class that lists the interface after the colon. No access modifier is used on explicit implementations.
Step-by-Step Solution:
Option D uses correct C#: class MyClass : IMyInterface { double IMyInterface.MyFun(Single i) { ... } }Option A uses non-existent “as” syntax.Option B uses VB-like “As” and omits modifiers/return type position.Option C uses VB-like “implements” keywords; not C#.Verification / Alternative check:Compile Option D: succeeds. Attempt A/B/C in a C# project: they fail with syntax errors.
Why Other Options Are Wrong:
- A/B/C borrow from other languages or invent syntax not supported by C#.
Common Pitfalls:Mixing VB and C# syntax when writing interface implementations; remember explicit implementations omit access modifiers and are qualified by the interface name.
Final Answer:class MyClass : IMyInterface { double IMyInterface.MyFun(Single i) { / code */ } }