Search code examples
c#internal

How internal specifier works


I just try to use Internal in my BaseClass, MSDN told me that the Internal Class can't be Instantiate in other method or class and produce an error, But why I can still Instantiate the BaseClass in method Main()?

TestAccess.cs

internal class BaseClass 
{
   public static int intM = 0;
}

class TestAccess 
{
   static void Main() 
   {
      BaseClass myBase = new BaseClass();   // CS0122 This should be an error because BaseClass is Internal class.
   }
}

Solution

  • You misunderstood MSDN. internal means that the class is only visible within the assembly that contains it. The example you posted is perfectly valid.

    MSDN says,

    It is an error to reference a type or a member with internal access outside the assembly within which it was defined.

    (emphasis mine)