03.01.2015 Views

C# 5.0 Programmer's Reference

Visual Studio 2013 C# 5.0 Programmer's Reference

Visual Studio 2013 C# 5.0 Programmer's Reference

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

248 ❘ CHAPTER 11 OOP Concepts<br />

A second analogy compares a class definition to a cookie cutter. After you create the cookie cutter, you<br />

can use it to make any number of cookies (instances). The cookie cutter (class) defines the cookies’ size<br />

and shape. Specific instances might have different properties such as thickness, dough type (chocolate<br />

chip, sugar, gingerbread, and so on), and frosting type (none, single color, and patterned).<br />

Classes, Classes Everywhere<br />

The .NET Framework is full of classes. Every type of control and component (Form,<br />

TextBox, Label, Timer, Window, and so forth) is a class. Their parent classes Control<br />

and Component are classes. The Framework also includes classes that represent containers,<br />

queues, messages, random number generators, web pages, XML documents,<br />

XPS documents, cryptographic functions, printers, operating system features, and all<br />

sorts of other things.<br />

Even Object, from which all other classes derive, is a class. Whenever you work<br />

with any of these (getting or setting properties, calling methods, and responding to<br />

events), you are working with instances of classes. In total, the .NET Framework<br />

contains somewhere on the order of 10,000 public classes.<br />

Actually, every piece of code you write in <strong>C#</strong> must be contained in a class. There’s<br />

no escaping classes!<br />

Because all classes ultimately derive from the Object class, every instance of every class is in some<br />

sense an Object, so they are often simply called objects. If you don’t know or don’t care about an<br />

item’s class, you can simply refer to it as an object.<br />

Outstanding Objects<br />

When you read the section “Polymorphism” later in this chapter, you’ll see that this<br />

makes technical, as well as intuitive, sense. Because all classes eventually derive from<br />

the Object class, all instances of all classes are actually Objects.<br />

The following sections provide more details about the more important features provided by OOP<br />

languages in general and <strong>C#</strong> in particular.<br />

Encapsulation<br />

A class’s public interface is the set of properties, methods, and events that are visible to code outside of<br />

the class. The class may also have private properties, methods, and events that it uses to do its job. For<br />

example, the Job class described in the previous section provides an AssignJob method. That method<br />

might call a private FindQualifiedEmployee method that looks through an employee database to<br />

find someone who has the skills and equipment necessary to do the job. That routine is not used outside<br />

of the class, so it can be declared private.<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!