Bird
0
0

Identify the error in this C# code:

medium📝 Debug Q6 of 15
C Sharp (C#) - Classes and Objects
Identify the error in this C# code:
class Test {
    public int value;
    public static void SetValue(int v) {
        value = v;
    }
}
AMissing return type for SetValue method.
BCannot assign to instance field 'value' inside static method.
CStatic methods cannot have parameters.
DNo error; code is correct.
Step-by-Step Solution
Solution:
  1. Step 1: Check static method accessing instance field

    The static method tries to assign to 'value', which is an instance field, without an object.
  2. Step 2: Understand C# rules

    Static methods cannot access instance fields directly; this causes a compile-time error.
  3. Final Answer:

    Cannot assign to instance field 'value' inside static method. -> Option B
  4. Quick Check:

    Static methods can't access instance fields directly [OK]
Quick Trick: Static methods can't access instance fields without object [OK]
Common Mistakes:
MISTAKES
  • Thinking static methods can access instance fields directly
  • Confusing method parameter rules
  • Assuming no error in code

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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