Bird
0
0

You want to create a class Book with an auto-implemented property Title that can be read publicly but only set privately within the class. Which code snippet correctly implements this?

hard🚀 Application Q15 of 15
C Sharp (C#) - Properties and Encapsulation
You want to create a class Book with an auto-implemented property Title that can be read publicly but only set privately within the class. Which code snippet correctly implements this?
Apublic class Book { public string Title { get; private set; } }
Bpublic class Book { private string Title { get; set; } }
Cpublic class Book { public string Title { private get; set; } }
Dpublic class Book { public string Title { get; set; private } }
Step-by-Step Solution
Solution:
  1. Step 1: Understand access modifiers for properties

    To allow public reading but private setting, the getter must be public and setter private: get; private set;.
  2. Step 2: Check each option's syntax and access

    public class Book { public string Title { get; private set; } } correctly uses public string Title { get; private set; }. public class Book { private string Title { get; set; } } makes the whole property private. public class Book { public string Title { private get; set; } } incorrectly places private before get. public class Book { public string Title { get; set; private } } has invalid syntax.
  3. Final Answer:

    public class Book { public string Title { get; private set; } } -> Option A
  4. Quick Check:

    Public get + private set = public class Book { public string Title { get; private set; } } [OK]
Quick Trick: Use 'get; private set;' for public read, private write [OK]
Common Mistakes:
MISTAKES
  • Placing private before get instead of set
  • Making whole property private
  • Incorrect syntax order for accessors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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