Bird
0
0

Find the error in this C# code:

medium📝 Debug Q7 of 15
C Sharp (C#) - Properties and Encapsulation
Find the error in this C# code:
class Example {
  private int _count;
  public int Count {
    get { return _count; }
    set { _count = value; }
  }

  public int Count;
}
AField '_count' should be public.
BProperty 'Count' must be static.
CDuplicate member 'Count' declared as property and field.
DNo error, code is valid.
Step-by-Step Solution
Solution:
  1. Step 1: Identify duplicate member names

    The class declares a property and a field both named Count, causing a conflict.
  2. Step 2: Understand C# member naming rules

    Members in a class must have unique names; duplicates cause compilation errors.
  3. Final Answer:

    Duplicate member 'Count' declared as property and field. -> Option C
  4. Quick Check:

    Property and field names must be unique [OK]
Quick Trick: Avoid duplicate names for fields and properties [OK]
Common Mistakes:
MISTAKES
  • Thinking property must be static
  • Assuming private fields must be public
  • Believing code compiles without error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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