Thursday, June 4, 2009

C# Puzzle No.17 (beginner)

Consider following code:

   1: using System;
   3: class Foo
   4: {
   5:     private Foo() { }
   6: }
   8: class Program : Foo
   9: {
  10:     static void Main( string[] args )
  11:     {
  12:     }
  13: }

The compiler says that

Foo() is inaccessible due to its protection level

But wait a sec, there's no need to complain since there's no attempt to create an instance of Foo!

In fact, the keyword "new" is never used in the code. It shouldn't then matter whether Foo is or is not accessible.

Your goal is to explain the compiler's complaint.


Deto said...

Hmm, 'Program' class has default parameterless contructor, thus - automatically call :base() in this ctor.

However we dont see it explicitely in code.

Thats my explanation and only idea why such happen.

Guest said...

No compiler doesn't return any error.
cause scop of variable occured during accessing it.