Bird
Raised Fist0

Examine the code below:

medium📝 Debug Q6 of Q15
C Sharp (C#) - Classes and Objects
Examine the code below:
class Sample {
  public static int count = 10;
  public void Display() {
    Console.WriteLine(this.count);
  }
}

What is the issue with using this.count inside the Display method?
AThere is no issue; the code works correctly
BStatic members cannot be accessed inside instance methods
CThe <code>count</code> field is not initialized
D<code>this</code> cannot be used to access static members
Step-by-Step Solution
Solution:
  1. Step 1: Understand static vs instance context

    Static members belong to the class, not instances.
  2. Step 2: Usage of this

    this refers to the current instance, so it cannot be used to access static members.
  3. Step 3: Correct access

    Static members should be accessed using the class name or directly by name inside instance methods.
  4. Final Answer:

    this cannot be used to access static members -> Option D
  5. Quick Check:

    Static members accessed without this [OK]
Quick Trick: this cannot access static members [OK]
Common Mistakes:
MISTAKES
  • Assuming this can access static fields
  • Confusing static and instance members
  • Ignoring compiler errors about this usage

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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