The Anatomy of a Class: Overview


Before we discuss each class member in detail, let's get an overview of the members we have available.

Syntax Box 12.1 expands the syntax shown in Figure 12.1 by including the total set of members we can include in the class definition. The first few lines displays the familiar class definition syntax consisting of the class keyword followed by the name of the class (identifier) and the set of curly braces that forms the class body where the class members reside. Further down, the class members have been divided into three broad categories data members, function members, and nested types. The following provides a brief introduction to the members of each of these categories, which also have been listed in the syntax box. Data members consist of member variables, constants, and events.

  • Member variables (also called fields) are used to represent data. A member variable can either belong to a particular instance (object) of a class, in which case it is called an instance variable, or it can belong to the class itself (by declaring it static), in which case it is called a static variable (or a class variable). Recall that a static method is called through its class (not its object) and belongs to a class not an object. Similarly, a static variable belongs to a class not any specific object.

  • A member variable can also be declared readonly (with the readonly keyword). readonly member variables are closely related to the constant data member, but holds an important difference the constant member's value is set to the value specified in the source code when the program is compiled and represents this value throughout the lifetime of the compiled program. In contrast, the readonly member is assigned its value when an object is created and only keeps this value during the lifetime of the object.

  • Events are declared in a similar fashion to that of member variables, but their use is very different. When you click a button (perhaps an OK button) on your graphical user interface (GUI), a corresponding object inside the program you are running generates (or fires) an event (perhaps called OKButtonClick) that will cause another part of the program to react and execute a piece of code. This type of program is called an event-driven program, because the next action taken by the program depends on the next event fired in the program. Consequently, the notion of a program executed in the sequence by which its statements are written is more or less gone. This asynchronous ability is necessary when running a GUI application, because the user at any one particular time has the choice between pointing and clicking many different buttons onscreen and the keyboard. Event-driven programming is not only needed for GUI application, but also for other important types of applications.

    Event-driven programming is an advanced subject, so the events discussion is deferred until Chapter 20, "Delegates and Events."

Syntax Box 12.1 Overview of the Class Members

 Class_definition::= class <Class_identifier> {     <Class_members> } 


 <Class_members>             ::= <Data_members>             ::= <Function_members>             ::= <Nested_types> <Data_members>            ::=    <Member_variables>            ::=    <Constants>            ::=    <Events> <Function_members>                ::= <Methods>                ::= <Constructors>                ::= <Destructor>                ::= <Properties>                ::= <Indexers>                ::= <Operators> <Nested_types>                   ::= <Nested_classes>                   ::= <Nested_structs>                   ::= <Nested_enums> 


  • The class definition provided here concentrates on the insides of the class and omits the syntax elements related to access modification, inheritance, and interfaces of the class itself.

  • The class members can be written inside the class in any order you want without changing the semantics.

  • A <Function_member> can either be an <Instance_function> or a <Static_function> (also called <Class_function>). An instance function is executed in relation to a particular object and, as opposed to the static function, needs an object to be called through.

Function members consist of methods, constructors, destructors, properties, indexers, and operators:

  • The method is already a familiar construct, but it still contains a number of unexplored facets, such as reference and output parameters, parameter arrays, method overloading, and the this keyword. A large part of this chapter is devoted to those important concepts.

  • Constructors have been informally presented previously and are thoroughly discussed in Chapter 13, "Class Anatomy Part II: Object Creation and Garbage Collection."

  • The destructor (also called finalizer) is a new concept that deserves a brief introduction here before we look closer at it in Chapter 13. We know that objects are created and allocated a piece of memory where the values of their instance variables and other data are stored. When an object is no longer needed in the program, it must be disposed of and its memory released for use by new objects being created; otherwise, the program could quickly run out of memory. A destructor, like its sibling the constructor, can contain a set of programmer-specified statements that are called by the runtime (it cannot be called directly from the source code) when this reclamation takes place.

  • Properties are accessed as if they are member variables, but contain statements, just like a conventional method, that are executed when the property is accessed. They are often used instead of accessor and mutator methods (that conventionally have names like GetDistance and SetDistance) to access private member variables and, therefore, support the encapsulation principle.

  • Indexers are used with classes that represent an array as their main purpose. Whereas properties allow convenient access to individual member variables, indexers allow convenient array-like access to an array residing inside a class.

  • Sometimes, it makes sense (and the code more readable) if two objects can be combined with an operator to form a well-defined result. For example, this could allow us to write statements like totalTime = myTime + yourTime; where all three values are objects of class TimeInterval. We can achieve this kind of behavior by including operator class members in our classes that specify the code to be executed when two objects are combined with the indicated operator. This process is often referred to as operator overloading.

Nested types are classes, structs, enums, and interfaces defined within the class body. They allow the programmer to hide types that are only needed within a class. Just like helper methods are kept private to lessen the complexity faced by the user of the class, nested types help us to reduce the number of classes to which an assembly user needs to look.


C# Primer Plus
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2000
Pages: 286
Authors: Stephen Prata

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: