internal Access

Classes can be declared with only two access modifierspublic and internal. If there is no access modifier in the class declaration, the class defaults to internal access. This allows the class to be used by all code in the same assembly as the class, but not by code in other assemblies. Within the same assembly as the class, this is equivalent to public access. However, if a class library is referenced from an application, the library's internal classes will be inaccessible from the code of the application. Similarly, methods, instance variables and other members of a class declared internal are accessible to all code compiled in the same assembly, but not to code in other assemblies.

The application in Fig. 9.20 demonstrates internal access. The application contains two classes in one source-code filethe InternalAccessTest application class (lines 622) and the InternalData class (lines 2543).

Figure 9.20. Members declared internal in a class are accessible by other classes in the same assembly.

(This item is displayed on page 446 in the print version)

1 // Fig. 9.20: InternalAccessTest.cs 2 // Members declared internal in a class are accessible by other classes 3 // in the same assembly. 4 using System; 5 6 public class InternalAccessTest 7 { 8 public static void Main( string[] args ) 9 { 10 InternalData internalData = new InternalData(); 11 12 // output string representation of internalData 13 Console.WriteLine( "After instantiation: {0}", internalData ); 14 15 // change internal access data in internalData 16 internalData.number = 77; 17 internalData.message = "Goodbye"; 18 19 // output string representation of internalData 20 Console.WriteLine( " After changing values: {0}", internalData ); 21 } // end Main 22 } // end class InternalAccessTest 23 24 // class with internal access instance variables 25 class InternalData 26 { 27 internal int number; // internal-access instance variable 28 internal string message; // internal-access instance variable 29 30 // constructor 31 public InternalData() 32 { 33 number = 0; 34 message = "Hello"; 35 } // end InternalData constructor 36 37 // return InternalData object string representation 38 public override string ToString() 39 { 40 return string.Format( 41 "number: {0}; message: {1}", number, message ); 42 } // end method ToString 43 } // end class InternalData  

After instantiation: number: 0; message: Hello After changing values: number: 77; message: Goodbye

In the InternalData class declaration, lines 2728 declare the instance variables number and message with the internal access modifierclass InternalData has access internal by default, so there is no need for an access modifier. The InternalAccessTest's static Main method creates an instance of the InternalData class (line 10) to demonstrate modifying the InternalData instance variables directly (as shown in lines 1617). Within the same assembly, internal access is equivalent to public access. The results can be seen in the output window. If we compile this class into a .dll class library file and reference it from a new application, that application will have access to public class InternalAccessTest, but not to internal class InternalData, or its internal members.

Категории