Bird
0
0

What will be the output of this C# code?

medium📝 Predict Output Q5 of 15
C Sharp (C#) - Properties and Encapsulation
What will be the output of this C# code?
class Person {
  private string _name = "Bob";
  public string Name {
    get { return _name; }
    set { _name = value.ToUpper(); }
  }
}

var p = new Person();
p.Name = "alice";
Console.WriteLine(p.Name);
ACompilation error
Balice
CBob
DALICE
Step-by-Step Solution
Solution:
  1. Step 1: Analyze property setter behavior

    The setter converts the assigned value to uppercase before storing it in _name.
  2. Step 2: Trace the assignment and output

    Assigning "alice" sets _name to "ALICE"; the getter returns "ALICE".
  3. Final Answer:

    ALICE -> Option D
  4. Quick Check:

    Property setter modifies value before storing [OK]
Quick Trick: Property setters can modify values before storing [OK]
Common Mistakes:
MISTAKES
  • Expecting original case without modification
  • Confusing field and property values
  • Assuming default value prints

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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