Bird
Raised Fist0

What will be the output of the following C# code?

medium📝 Predict Output Q13 of Q15
C Sharp (C#) - Properties and Encapsulation

What will be the output of the following C# code?

class User {
    private string _name = "Alice";
    public string Name { get { return _name; } }
}

var user = new User();
Console.WriteLine(user.Name);
ACompilation error
Bnull
CRuntime error
DAlice
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the property

    The Name property is read-only with a get accessor returning "Alice".
  2. Step 2: Check output of Console.WriteLine

    Calling user.Name returns "Alice", so it prints "Alice".
  3. Final Answer:

    Alice -> Option D
  4. Quick Check:

    Read-only property returns stored value [OK]
Quick Trick: Read-only property returns stored value when accessed [OK]
Common Mistakes:
MISTAKES
  • Expecting a compilation error due to missing set
  • Thinking write-only properties can be read
  • Confusing private field with property access

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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