C#.NET properties — Which statement correctly describes properties?

Difficulty: Easy

Correct Answer: A property can be either read only or write only.

Explanation:


Introduction / Context:
Properties are a core C# feature that encapsulate access to fields. The presence of get and/or set accessors determines whether a property is readable, writable, or both. This question asks for the correct generic statement about property capabilities.



Given Data / Assumptions:

  • We consider standard auto-properties or manually implemented properties.
  • “Read only” means get without set; “write only” means set without get.


Concept / Approach:
A property can be read-only (get; no set;), write-only (set; no get;), or read–write (both get and set). A write-only property does not return a value; it simply accepts a value in its set accessor via the implicit parameter value. Any statement claiming a property is simultaneously read only and write only is self-contradictory.



Step-by-Step Solution:

Evaluate option B: Accurately describes that a property can be either read only or write only (or both, though not stated).Option A: Logically impossible to be both read only and write only.Option C: Inverts accessors (write-only would have set, not get).Option D: Says a write-only property “returns a value,” which it does not.


Verification / Alternative check:
Code examples: public int Id { get; } (read-only), public int Secret { set { ... } } (write-only), public int Count { get; set; } (read–write).



Why Other Options Are Wrong:

  • A/C/D contradict accessor semantics.


Common Pitfalls:
Assuming write-only properties are common; they are rare because callers cannot read back a value. Use scenarios include sensitive data, logging, or command-like APIs.



Final Answer:
A property can be either read only or write only.

Discussion & Comments

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