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
|
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.
Preface
Index
Introduction to Computers, the Internet and Visual C#
Introduction to the Visual C# 2005 Express Edition IDE
Introduction to C# Applications
Introduction to Classes and Objects
Control Statements: Part 1
Control Statements: Part 2
Methods: A Deeper Look
Arrays
Classes and Objects: A Deeper Look
Object-Oriented Programming: Inheritance
Polymorphism, Interfaces & Operator Overloading
Exception Handling
Graphical User Interface Concepts: Part 1
Graphical User Interface Concepts: Part 2
Multithreading
Strings, Characters and Regular Expressions
Graphics and Multimedia
Files and Streams
Extensible Markup Language (XML)
Database, SQL and ADO.NET
ASP.NET 2.0, Web Forms and Web Controls
Web Services
Networking: Streams-Based Sockets and Datagrams
Searching and Sorting
Data Structures
Generics
Collections
Appendix A. Operator Precedence Chart
Appendix B. Number Systems
Appendix C. Using the Visual Studio 2005 Debugger
Appendix D. ASCII Character Set
Appendix E. Unicode®
Appendix F. Introduction to XHTML: Part 1
Appendix G. Introduction to XHTML: Part 2
Appendix H. HTML/XHTML Special Characters
Appendix I. HTML/XHTML Colors
Appendix J. ATM Case Study Code
Appendix K. UML 2: Additional Diagram Types
Appendix L. Simple Types
Index