Sie sind auf Seite 1von 29

Introduction to Classes

and objects

• Learn about class concepts
• How to create a class from which objects can
be instantiated
• Learn about instance variables and methods
• How to declare objects

Understanding Class Concepts

• There are two distinct types of classes created in C#:

– Classes that contain a Main() method
– Classes from which you instantiate objects
• Everything is considered an object in the object-oriented
• Object-oriented programmers recognize “is-a

Understanding Class Concepts

• The use of objects provides a better understanding of

the idea that is being programmed
• The data components of a class are called the instance
variables of that class
• Class object attributes are called fields
• The contents of a class object’s instance variables are
also known as its state
• In addition to attributes, class objects also have methods
associated with them

Creating a Class from Which Objects Can
Be Instantiated
• A class header or class definition contains
three parts:
– An optional access modifier
– The keyword class
– Any legal identifier you choose for the name of your

Creating a Class from Which Objects Can
Be Instantiated

• Private and protected classes have limited uses

• When you declare a class using a namespace,
you can only declare it to be public or internal
• Class access is internal by default

Creating Instance Variables and Methods

• Instance variables are created using the same

syntax used to declare other variables
• The allowable field modifiers are new, public,
protected, internal, private, static, readonly, and

Creating Instance Variables and Methods

• Identifying a field as private means that no other class

can access the field’s value, and only methods of the
same class will be allowed to set, get, or otherwise use
the field
• Using private fields within classes is an example of
information hiding
• Although most fields are private, most class methods are

Creating Instance Variables and Methods

• In the following code, the variable idNumber is private, yet

remains accessible to outside classes through the GetId()
• Methods used with object instantiations are called instance

Creating Instance Variables and Methods

• Figure 4-4 creates a method that is a counterpart to the GetID()

method, called SetId()
• The SetId() method does not return any value, rather, it is used
to assign a value in the class

Declaring Objects

• Declaring a class does not create any actual objects

• A two step process creates an object that is an instance
of a class:
– Supply a type and an identifier
– Allocate computer memory for the object
• Declaring an object notifies the compiler of the identifier
and the type, but it does not allocate memory

Declaring Objects

• To allocate the needed memory for an object, use the

new operator
• For example:
Employee myAssistant = new Employee();
• The value being assigned to myAssistant is a memory
address at which it will be located
• Any class created can be referred to as a reference
type, while the predefined types are called value types
• The method called after the new keyword is called the
constructor method

Compiling and Running a Program That
Instantiates Class Objects
• When you create an application that uses multiple class
definitions, the class definitions can be stored in either a
single file or each can be placed in its own file
• Storing all class definitions in one file shortens
development time and simplifies the compilation process
• Using separate files for each class definition provides a
more organized and manageable technique, and it
allows for easier reusability

Organizing Your Classes

• Most classes you create will have multiple methods and

fields, which can become difficult to track in large
• It is a good idea to arrange fields and methods in a
logical order:
– Alphabetically
– By “Sets” and “Gets”
– Pairing of “Sets” and “Gets”

Organizing Your Classes

• In addition to organizing fields and methods, comments

should also be used
Using Public Fields and Private Methods
• The private fields/public method arrangement ensures
that data will be used and changed only in ways
provided in your methods
• Although it is easier to declare fields as public, doing so
is considered poor object-oriented programming design
• Data should always be hidden when possible and
access should be controlled by well-designed methods

Using Public Fields and Private Methods

• Poorly designed Desk class and program that instantiates a Desk

Using Public Fields and Private Methods

• The Carpet class

Using Public Fields and Private Methods

• Occasionally, there are instances where a data field

should be declared as public
• A data field may be declared public if all objects of a
class contain the same value for that data field
• The preceding example (fig 4-12) declared a public
const field
• A named constant within a class is always static

Understanding the this Reference

• When instances of a class are created, the resulting

objects require separate memory locations in the
• In the following code, each object of Book would at least
require separate memory locations for title, numPages,
and Price

Understanding the this Reference

• Unlike the class fields that are unique to each instance,

the methods of the class are shared
• Although the methods are shared, there must be a
difference between two method calls (from different
objects), because each returns a different value (based
on their unique fields)
• When a method is called, you automatically pass a
reference to the memory of the object into the method
• The implicitly passed reference is the this reference

Understanding the this Reference

• Book class methods explicitly using the this reference

Understanding the this Reference

• Book class method that requires explicit this reference

Understanding Constructor Methods

• A constructor method is a method that establishes an

• Every class created is automatically supplied with a
public constructor method with no parameters
• Any constructor method you write must have the same
name as its class, and constructor methods cannot have
a return type

Passing Parameters to Constructors

• You can create a constructor that sets the same value

for all objects instantiated. The program can eventually
call the method of the object to set the value. However,
this might not always be the most efficient technique.

Passing Parameters to Constructors

• In this code, the constructor receives an argument and

initializes the object with the appropriate information

Overloading Constructors

• C# automatically provides a default constructor

• However if you create your own constructor, the
automatically provided default constructor is not
• C# constructor methods, like other methods, can be

Understanding Destructor Methods

• A destructor method contains the actions you require

when an instance of a class is destroyed
• To explicitly declare a destructor method, use an
identifier that consists of a tilde(~) followed by the class
• Destructors cannot receive parameters and therefore
cannot be overloaded
• A class can have at most one destructor

Understanding Destructor Methods

• Employee class with destructor method