I am getting this error:
‘CTest.A.A()’ is inaccessible due to its protection level.
when compiling this code:
public class A
{
private A()
{
}
}
public class B : A
{
public void SayHello()
{
Console.WriteLine("Hello");
}
}
Can anyone explain why?
Because the default constructor for A is private, try
protected A() {}as the constructor.Class
Bautomatically calls the default constructor ofA, if that is inaccessible toBor there is no default constructor (if you have constructorprotected A(string s) {})Bcan not be instantiated correctly.The compiler automatically generates the following default constructor in
BWhere
base()is the actual call to the default constructor ofA.