Bird
0
0

Which of the following is the correct syntax to declare a property named Age with a private field in C#?

easy📝 Syntax Q12 of 15
C Sharp (C#) - Properties and Encapsulation
Which of the following is the correct syntax to declare a property named Age with a private field in C#?
Aint Age; int Age { get; set; }
Bprivate int Age; public int Age { get; set; }
Cpublic int age; public int Age;
Dprivate int age; public int Age { get { return age; } set { age = value; } }
Step-by-Step Solution
Solution:
  1. Step 1: Identify private field declaration

    The private field should be lowercase (e.g., age) and declared as private int age;.
  2. Step 2: Identify property syntax

    The property Age uses get and set to access the private field correctly.
  3. Final Answer:

    private int age; public int Age { get { return age; } set { age = value; } } -> Option D
  4. Quick Check:

    Private field + property with get/set = correct syntax [OK]
Quick Trick: Private field lowercase, property uppercase with get/set. [OK]
Common Mistakes:
MISTAKES
  • Using same name for field and property causing errors.
  • Missing get or set in property.
  • Declaring fields as public when they should be private.

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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