C#.NET — Can a static method access instance data directly?
-
AYes
-
BNo
-
C—
-
D—
-
E—
Answer
Correct Answer: No
Explanation
Introduction / Context:Understanding the difference between static and instance context is essential in C#. Static methods belong to the type itself, whereas instance members belong to specific objects.
Given Data / Assumptions:
- Static methods do not have an implicit
this. - Instance fields/properties require an instance to reference.
Concept / Approach:A static method cannot directly access instance data because it lacks a particular object. It may only reach instance data if an instance reference is provided as a parameter or obtained in some other explicit way.
Step-by-Step Solution:
1) Static method context → nothis.2) Instance field requires object reference.3) Therefore, direct access is not allowed; only static members are accessible without an instance.Verification / Alternative check:Try compiling a static method that reads an instance field without an instance; the compiler errors.
Why Other Options Are Wrong:
- Yes: Would require an implicit instance, which static methods do not have.
Common Pitfalls:Calling a static method through a variable of the class type and assuming that gives it instance access — it does not.
Final Answer:No