I am reading book "C# 4.0 in a nutshell" by Joseph Albabari and Ben Albabari. From there I find a topic restrictions on access modifiers. Page 91, Topic "Restrictions on Access Modifiers".
Quoting from the book.
The compiler prevents any inconsistent use of access modifiers. For example, a sub- class itself can be less accessible than a base class, but not more
So this says that base class should be equally or more accessible than subclass. So if base class is internal then subclass should be either private or internal. If base class is private and sub class is public then compile time error will be generated. While trying this in Visual Studio I found some strange behavior.
Try 1: Base is private and sub class is private (Works, right behavior) This also works if both are internal, public.
private class A { }
private class B : A { } // Works
Try 2: Base is private and sub class is public or internal (This fails, right behavior)
private class A { }
public class B : A { } // Error
Try 3 : Base is internal and sub is public (This works, but it should fail. As Base is less accessible than sub class
internal class A { }
public class B : A { } // Works, but why
Now my question is why Try 3 didn't failed? Sub class is public and is more accessible than base class which is internal. Even the book says this should fail. But Visual Studio compiled this successfully. This should work or not?
Edit:
I created a new Console Project in VS. In Program.cs I added my code. Here is complete code of Program.cs file.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsoleApplication
{
class Program
{
internal class A { }
public class B : A { } // Error
static void Main()
{
}
}
}