Bird
0
0

Find the error in this code:

medium📝 Debug Q7 of 15
C Sharp (C#) - Classes and Objects
Find the error in this code:
internal class Data {
    private int value;
}
class Program {
    static void Main() {
        Data d = new Data();
        d.value = 10;
    }
}
ANo error, code is correct
BCannot access private field 'value' outside class Data
CMissing public modifier on class Program
DCannot create instance of internal class Data
Step-by-Step Solution
Solution:
  1. Step 1: Check access to private field

    The field 'value' is private inside Data, so it cannot be accessed from Program.
  2. Step 2: Confirm instance creation is allowed

    Data is internal, accessible within same assembly, so creating instance is allowed.
  3. Final Answer:

    Cannot access private field 'value' outside class Data -> Option B
  4. Quick Check:

    Private fields inaccessible outside their class [OK]
Quick Trick: Private fields block access even within same assembly [OK]
Common Mistakes:
MISTAKES
  • Confusing internal with private
  • Thinking internal blocks instance creation
  • Assuming no access restrictions on fields

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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