Chapter 4. Classes and Objects
Chapter 3
discusses
the myriad primitive 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.
It is this ability to create new types that characterizes an object-oriented language. You specify new types in C# by declaring and defining classes. 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.
A Dog class 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, bark,
and sleep. A particular dog (such as my dog Milo) will have a
specific weight (62 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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access