Bird
0
0

Examine this property setter:

medium📝 Debug Q7 of 15
C Sharp (C#) - Properties and Encapsulation
Examine this property setter:
private string _title;
public string Title {
  get => _title;
  set {
    if (string.IsNullOrWhiteSpace(value))
      throw new ArgumentException("Title cannot be empty");
    _title = value;
  }
}

What happens if the setter is called with an empty string?
AThe setter ignores the empty string and leaves _title unchanged
BThe empty string is assigned to _title
CAn ArgumentException is thrown preventing assignment
DThe setter assigns null to _title
Step-by-Step Solution
Solution:
  1. Step 1: Analyze validation condition

    The setter checks if the value is null, empty, or whitespace and throws an exception.
  2. Step 2: Understand behavior on empty string

    Empty string triggers the condition, so an ArgumentException is thrown.
  3. Step 3: Confirm assignment

    Since exception is thrown, assignment does not occur.
  4. Final Answer:

    An ArgumentException is thrown preventing assignment -> Option C
  5. Quick Check:

    Empty input triggers exception [OK]
Quick Trick: Empty or whitespace input throws exception [OK]
Common Mistakes:
MISTAKES
  • Assuming empty string is assigned
  • Thinking setter ignores invalid input silently
  • Confusing null and empty string handling

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More C Sharp (C#) Quizzes