Difficulty: Easy
Correct Answer: Sample m = new Sample(); int l; l = m.Length;
Explanation:
Introduction / Context:
A read-only property exposes only a getter. This permits reading the value but forbids assignment. Many .NET APIs use read-only properties for computed or immutable values.
Given Data / Assumptions:
Concept / Approach:
With only a getter, you can read the property into a local variable or print it using the instance. You cannot assign to it, nor can you use type-qualified syntax like Sample.Length unless the property is static. Therefore, the only correct option is to construct an instance and read the property value.
Step-by-Step Solution:
Verification / Alternative check:
Define public int Length { get { return _len; } } and compile. A and B produce compile-time errors, while C compiles.
Why Other Options Are Wrong:
Common Pitfalls:
Confusing static and instance members; always access instance properties through an instance unless explicitly static.
Final Answer:
Sample m = new Sample(); int l; l = m.Length;
Discussion & Comments