Difficulty: Easy
Correct Answer: Declare the age property with only a set accessor (write-only property).
Explanation:
Introduction / Context:
This question examines how C#.NET properties control read/write accessibility. The goal is to make a read attempt Console.WriteLine(emp.age) fail at compile time, so you must deny the public getter while still possibly allowing writes.
Given Data / Assumptions:
Concept / Approach:
In C#, a property can expose a get (read) accessor, a set (write) accessor, both, or neither (with restricted access). A write-only property is made by providing only set and omitting get (or making get less accessible).
Step-by-Step Solution:
Verification / Alternative check:
Try compiling with only set present: any read usage fails, while writes like emp.age = 30; compile.
Why Other Options Are Wrong:
Common Pitfalls:
Confusing access modifiers on get/set with presence of accessors. Even with access modifiers, any accessible get will allow reading.
Final Answer:
Declare the age property with only a set accessor (write-only property).
Discussion & Comments