C#.NET — Is the following interface definition valid? interface IPerson { string FirstName { get; set; } string LastName { get; set; } void Print(); void Stock(); int Fun(); }

Difficulty: Easy

Correct Answer: This is a perfectly workable interface.

Explanation:


Introduction / Context:
Interfaces in C# define member signatures without implementation. This problem verifies whether properties and methods can be declared in an interface and whether bodies are required.



Given Data / Assumptions:

  • IPerson declares two properties and three methods.
  • No method or property accessor includes a body.


Concept / Approach:
In classic C#, interface members have signatures only. Implementing types must provide bodies. Properties in an interface are declared with get; and/or set; accessors — again, without bodies. Methods (void or returning values) are allowed and, similarly, have no bodies in the interface.



Step-by-Step Solution:

Check properties FirstName/LastName → valid with get; set; signatures only.Check methods Print/Stock/Fun → valid method signatures; no bodies necessary.Therefore the interface compiles and is usable as a contract for implementing types.


Verification / Alternative check:
Create a class Employee : IPerson and implement each member. The compiler will require bodies in the class.



Why Other Options Are Wrong:

  • A/E are false because interfaces can declare properties and methods.
  • C/D are false because bodies are not provided in interfaces (for this syllabus; default interface implementations came later and are not assumed here).


Common Pitfalls:
Trying to put code into interface accessors or methods; implementation belongs in classes/structs that implement the interface.



Final Answer:
This is a perfectly workable interface.

Discussion & Comments

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