C#.NET — Which statements about static methods are correct?
-
AStatic functions are invoked using objects of a class.
-
BStatic functions can access static data as well as instance data.
-
CStatic functions are outside the class scope.
-
DStatic functions are invoked using class.
-
E—
Answer
Correct Answer: Static functions are invoked using class.
Explanation
Introduction / Context:Static members belong to the type itself rather than to any particular instance. Understanding how to call them and what they can access is a core C# concept.
Given Data / Assumptions:
- We distinguish invocation style and data access rules.
- We focus on standard C# semantics without unsafe tricks.
Concept / Approach:Static methods are called on the type (for example, Math.Abs(…)), not on an instance. A static method cannot access instance fields or properties directly because it lacks a this reference. Static methods are defined within class (or struct) scope just like instance methods.
Step-by-Step Solution:
Evaluate A: False — static calls do not require (and should not use) an instance.Evaluate B: False — static methods cannot access instance data without an explicit instance reference passed in or obtained elsewhere.Evaluate C: False — static methods are members of a class; they are not “outside” the class.Evaluate D: True — the idiomatic and correct invocation is via the class:TypeName.StaticMethod().Verification / Alternative check:Attempt to reference an instance field from a static method; the compiler flags an error due to missing instance context.
Why Other Options Are Wrong:
- A: Confuses static with instance invocation.
- B: Violates access rules for static versus instance context.
- C: Misstates the scope where static members live.
Common Pitfalls:Creating an instance only to call a static method — unnecessary and misleading.
Final Answer:Static functions are invoked using class.