Classes and Objects
(Page 1 of 4 )
If you're looking for information on C#, you've come to the right place. This article focuses on classes and objects, concepts that are highly important to the increasingly popular programming language. It is excerpted from chapter four of the book
Programming C# 3.0, fifth edition, written by Jesse Liberty and Donald Xie (O'Reilly, 2008; ISBN: 0596527438). This is the first part of a four-part series. Copyright © 2008 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.
In Chapter 3, I discussed the myriad types built into the C# language, such as int, long, and char. The heart and soul of C#, however, is the ability to create new, complex, programmer-defined types that map cleanly to the objects that make up the problem you are trying to solve, and to use the programmer-defined types that Microsoft has provided in the Framework to facilitate creating applications without having to “reinvent the wheel” to accomplish common tasks such as interacting with the user, databases, web sites, and so forth.
It is this ability to use and create powerful new types that characterizes an object-oriented language. You specify a new type in C# by defining a class. (You can also define types with interfaces, as you will see in Chapter 8.) Instances of a class are called objects. Objects are created in memory when your program executes.
The difference between a class and an object is the same as the difference between the concept of a dog and the particular dog who is sitting at your feet as you read this. You can’t play fetch with the definition of a dog, only with an instance.
ADogclass describes what dogs are like: they have weight, height, eye color, hair color, disposition, and so forth. They also have actions they can take, such as eat, walk, (eat), bark, (eat some more), and sleep. A particular dog (such as Jesse’s dog Milo) has a specific weight (68 pounds), height (22 inches), eye color (black), hair color (yellow), disposition (angelic), and so forth. He is capable of all the actions of any dog (though if you knew him you might imagine that eating is the only method he implements).
The huge advantage of classes in object-oriented programming is that they encapsulate the characteristics and capabilities of an entity in a single, self-contained, and self-sustaining unit of code. When you want to sort the contents of an instance of a Windows listbox control, for example, you tell the listbox to sort itself. How it does so is of no concern to anyone but the person writing the listbox control; that the list-box can be sorted is all any other programmer needs to know. Encapsulation (the idea that an object is self-contained), along with polymorphism and inheritance (explained in just a moment), are the three cardinal principles of object-oriented programming.
An old programming joke asks “how many object-oriented programmers does it take to change a light bulb?” Answer: none, you just tell the light bulb to change itself.
This chapter explains the C# language features that are used to create new types by creating classes. It will demonstrate how methods are used to define the behaviors of the class, and how the state of the class is accessed through properties, which act like methods to the developer of the class but look like fields to clients of the class. The elements of the class—its behaviors and properties—are known collectively as its class members.
Next: Defining Classes >>
More C# Articles
More By O'Reilly Media