Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

Accessibility Domain

The accessibility domain of a member specifies where, in the program sections, a member can be referenced. If the member is nested within another type, its accessibility domain is determined by both the accessibility level of the member and the accessibility domain of the immediately containing type.

The accessibility domain of a top-level type is at least the program text of the project in which it is declared. That is, the entire source files of this project. The accessibility domain of a nested type is at least the program text of the type in which it is declared. That is, the type body, including any nested types. The accessibility domain of a nested type never exceeds that of the containing type. These concepts are demonstrated in the following example.

For more information, see 3.5.2 Accessibility domains.

Example

This example contains a top-level type, T1, and two nested classes, M1 and M2. The classes contain fields with different declared accessibilities. In the Main method, a comment follows each statement to indicate the accessibility domain of each member. Notice that the statements that attempt to reference the inaccessible members are commented out. If you want to see the compiler errors caused by referencing an inaccessible member, remove the comments one at a time.

// cs_Accessibility_Domain.cs
using System;
namespace MyNameSpace 
{
   public class T1 
   {
      public static int myPublicInt;
      internal static int myInternalInt;
      private static int myPrivateInt = 0;

      public class M1 
      {
         public static int myPublicInt;
         internal static int myInternalInt;
         private static int myPrivateInt = 0;
      }

      private class M2 
      {
         public static int myPublicInt = 0;
         internal static int myInternalInt = 0;
         private static int myPrivateInt = 0;
      }
   }

   public class MainClass 
   {
      public static int Main() 
      {
         // Access to T1 fields:
         T1.myPublicInt = 1;      // Access is unlimited
         T1.myInternalInt = 2;    // Accessible only in current assembly
         // T1.myPrivateInt = 3;  // Error: inaccessible outside T1
         
         // Access to the M1 fields:
         T1.M1.myPublicInt = 1;     // Access is unlimited
         T1.M1.myInternalInt = 2;   // Accessible only in current assembly
         // T1.M1.myPrivateInt = 3; // Error: inaccessible outside M1
         
         // Access to the M2 fields:
         // T1.M2.myPublicInt = 1;   // Error: inaccessible outside T1
         // T1.M2.myInternalInt = 2; // Error: inaccessible outside T1
         // T1.M2.myPrivateInt = 3;  // Error: inaccessible outside M2
      
         return 0;
      }
   }
}

See Also

Access Modifiers | Accessibility Levels | Restrictions on Using Accessibility Levels | 10.2.3 Access modifiers | 3.5.2 Accessibility domains

Show:
© 2015 Microsoft