C#.NET — Which is a correct implementation of the IPerson interface? interface IPerson { string FirstName { get; set; } }

Difficulty: Easy

Correct Answer: class Employee : IPerson\n{\n private string str;\n public string FirstName\n {\n get { return str; }\n set { str = value; }\n }\n}

Explanation:


Introduction / Context:
The problem asks you to identify correct C# syntax for implementing a simple interface property. It also tests awareness of explicit vs. implicit interface implementation and the correct inheritance syntax.



Given Data / Assumptions:

  • IPerson declares string FirstName { get; set; }.
  • We want a valid class that compiles and implements the property.


Concept / Approach:
To implement an interface implicitly, the class must list the interface after its name using a colon and then provide a public member with the same signature. Explicit interface implementation would use the qualified name IPerson.FirstName, but the class must still declare that it implements IPerson.



Step-by-Step Solution:

Option A: Correct — uses : IPerson and a public property matching the signature.Option B: Attempts explicit naming but the class does not implement IPerson; also access modifiers are invalid for explicit interface members in this form.Option C: Uses non-C# keyword “implements”; invalid syntax.


Verification / Alternative check:
Compile Option A — succeeds. Compile B/C — fail with syntax/interface errors.



Why Other Options Are Wrong:
B lacks : IPerson and misuses explicit implementation syntax; C is not C# syntax.



Common Pitfalls:
Confusing VB “Implements” with C#’s colon syntax, and forgetting that explicit property implementations need the interface qualification and no access modifiers.



Final Answer:
class Employee : IPerson { ... public string FirstName { get; set; } }

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion