Information Technology Reference
In-Depth Information
Overview of Classes
In the previous chapter, you saw that C# provides six user-defined types. The most important
of these, and the one I will cover first, is the
class
. Since the topic of classes in C# is a large one,
its discussion will be spread over the next several chapters.
A Class Is an Active Data Structure
Before the days of object-oriented analysis and design, programmers thought of a program as
just a sequence of instructions. The focus at that time was on structuring and optimizing those
instructions. With the advent of the object-oriented paradigm, the focus changed from opti-
mizing instructions to organizing a program's data and functions into encapsulated sets of
logically related data items and functions, called classes.
A class is a data structure that can store data and execute code. It contains the following:
Data members
, which store data associated with the class or an instance of the class.
Data members generally model the attributes of the real-world object the class
represents.
Function members
, which execute code. Function members generally model the func-
tions and actions of the real-world object that the class represents.
A C# class can have any number of data and function members. The members can be any
combination of nine possible member types. These member types are shown in Table 4-1. The
ones I will cover in this chapter—
fields
and
methods
—are checked in the table.
Table 4-1.
Types of Class Members
Data Members Store Data
Function Members Execute Code
✓
Fields
✓
Methods
❏
Operators
❏
Constants
❏
Properties
❏
Indexers
❏
Constructors
❏
Events
❏
Finalizers
■
Note
Classes are encapsulated sets of logically related data items and functions that generally represent
objects in the real world or a conceptual world.