Bird
0
0

What will be the output of this C# code?

medium📝 Predict Output Q13 of 15
C Sharp (C#) - Properties and Encapsulation
What will be the output of this C# code?
class Person {
    public string Name { get; set; }
}

var p = new Person();
p.Name = "Alice";
Console.WriteLine(p.Name);
Anull
BAlice
CName
DCompilation error
Step-by-Step Solution
Solution:
  1. Step 1: Understand property assignment

    The property Name is auto-implemented, so it stores the value "Alice" when assigned.
  2. Step 2: Output the property value

    When Console.WriteLine(p.Name); runs, it prints the stored string "Alice".
  3. Final Answer:

    Alice -> Option B
  4. Quick Check:

    Property stores "Alice" = output "Alice" [OK]
Quick Trick: Auto-properties store and return assigned values [OK]
Common Mistakes:
MISTAKES
  • Expecting property name instead of value
  • Thinking default null prints as text
  • Assuming syntax error without reason

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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