Bird
Raised Fist0

What happens if you try to access an instance member from a static method in C#?

easy🧠 Conceptual Q2 of Q15
C Sharp (C#) - Classes and Objects
What happens if you try to access an instance member from a static method in C#?
AIt works normally without any error.
BIt causes a compile-time error.
CIt throws a runtime exception.
DIt automatically creates an instance to access the member.
Step-by-Step Solution
Solution:
  1. Step 1: Recall static method restrictions

    Static methods do not have access to instance members because they lack an object context.
  2. Step 2: Understand compiler behavior

    Trying to access instance members inside static methods causes a compile-time error.
  3. Final Answer:

    It causes a compile-time error. -> Option B
  4. Quick Check:

    Static method cannot access instance members [OK]
Quick Trick: Static methods can't access instance members directly [OK]
Common Mistakes:
MISTAKES
  • Assuming runtime error instead of compile-time
  • Thinking static methods create instances automatically
  • Confusing static and instance method access rules

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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