Bird
0
0

What is a key advantage of using a property instead of a field in C#?

easy🧠 Conceptual Q2 of 15
C Sharp (C#) - Properties and Encapsulation
What is a key advantage of using a property instead of a field in C#?
AProperties are faster to declare than fields.
BProperties allow validation when setting values.
CProperties cannot be accessed outside the class.
DProperties use less memory than fields.
Step-by-Step Solution
Solution:
  1. Step 1: Understand the purpose of properties

    Properties provide controlled access to data, allowing code to run when getting or setting values.
  2. Step 2: Identify the advantage

    Validation or logic can be added inside property setters, which is not possible with simple fields.
  3. Final Answer:

    Properties allow validation when setting values. -> Option B
  4. Quick Check:

    Properties = Controlled access with validation [OK]
Quick Trick: Properties can run code on get/set, fields cannot [OK]
Common Mistakes:
MISTAKES
  • Assuming properties use less memory
  • Believing properties are always private
  • Thinking properties are faster to declare

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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