Table 3-1 provides a summary of the types that comprise a .NET class. They can be classified broadly as members that hold data constants, fields, and properties and members that provide functionality the constructor, method, and event. We'll look at each individually. Table 3-1. Class MembersMember Type | Valid In | Description |
---|
Constant | Class, Structure | A symbol that represents an unchanging value. The compiler associates it with the class not an instance of the class. | Field | Class, Structure | A variable that holds a data value. It may be read-only or read/write. | Property | Class, Structure | Provides access to a value in a class. It uses an accessor that specifies the code to be executed in order to read or write the value. The code to read or write to a property is implemented implicitly by .NET as two separate methods. | Constructor | Class, Structure | C# has three types of constructors: Instance. Initializes fields when an instance of a class is created. Private. Commonly used to prevent instances of a class from being created. Static. Initializes class before any instance is created. | Method | Class, Structure, Interface | A function associated with the class that defines an action or computation. | Events | Class, Structure, Interface | A way for a class or object to notify other classes or objects that its state has changed. | Types | Class, Structure, Interface | Classes, interfaces, structs, delegates. | Member Access Modifiers The access modifiers used for a class declaration can also be applied to class members. They determine the classes and assemblies that have access to the class. Table 3-2 summarizes the scope of accessibility. Table 3-2. Summary of Accessibility Provided by Access Modifiers | Access Modifiers |
---|
Class can be accessed by classes in: | Public | protected | Internal | private |
---|
Another assembly | Yes | [*] | No | [*] | Same assembly | Yes | [*] | Yes | [*] | Containing class | Yes | Yes | Yes | Yes | Class derived from containing class | Yes | Yes | Yes | No |
[*] Not applicable |