Chapter 02 Creating and Using Classes in C#
Chapter 02 Creating and Using Classes in C#
1
Objectives
• 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
2
Objectives
3
Objectives
4
Understanding Class Concepts
5
Understanding Class Concepts
6
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
class
7
Creating a Class from Which Objects Can
Be Instantiated
8
Creating Instance Variables and Methods
9
Creating Instance Variables and Methods
10
Creating Instance Variables and Methods
11
Creating Instance Variables and Methods
12
Declaring Objects
13
Declaring Objects
14
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
15
Organizing Your Classes
16
Organizing Your Classes
18
Using Public Fields and Private Methods
21
Understanding the this Reference
22
Understanding the this Reference
23
Understanding the this Reference
24
Understanding the this Reference
25
Understanding Constructor Methods
26
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.
27
Passing Parameters to Constructors
28
Overloading Constructors
29
Understanding Destructor Methods
30
Understanding Destructor Methods
32
Chapter Summary
34
Chapter Summary
35