Bird
Raised Fist0

Given this code snippet, identify the error and fix it:

medium📝 Debug Q14 of Q15
C Sharp (C#) - Classes and Objects

Given this code snippet, identify the error and fix it:

class Sample {
    internal int value;
}

class Test {
    void Show() {
        Sample s = new Sample();
        Console.WriteLine(s.value);
    }
}

Assuming these classes are in different projects, what is the problem?

ANo error, code runs fine
BError: 'value' is inaccessible due to protection level; fix by changing 'internal' to 'public'
CError: 'value' must be private; fix by changing 'internal' to 'private'
DError: 'value' must be static; fix by adding 'static' keyword
Step-by-Step Solution
Solution:
  1. Step 1: Understand 'internal' access modifier scope

    internal allows access only within the same project or assembly.
  2. Step 2: Check class locations

    Since Sample and Test are in different projects, Test cannot access internal members of Sample.
  3. Step 3: Fix the access level

    Changing value to public allows access from other projects.
  4. Final Answer:

    Error: 'value' is inaccessible due to protection level; fix by changing 'internal' to 'public' -> Option B
  5. Quick Check:

    Internal = same project only; public = accessible everywhere [OK]
Quick Trick: Internal limits access to same project; use public for cross-project [OK]
Common Mistakes:
MISTAKES
  • Assuming internal allows cross-project access
  • Changing internal to private incorrectly
  • Confusing static with access modifiers

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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