Chapter 5. Classes
You briefly saw in Chapter 1 how to declare a new class called HelloWorld. In Chapter 2, you learned about the
built-in primitive types included with C#. Since you have now also learned about control flow and how to declare
methods, it is time to discuss defining your own types. This is the core construct of any C# program, and the
complete support for classes and the objects created from them is what defines C# as an object-oriented language.
[View full size image]
This chapter introduces you to the basics of object-oriented programming using C#. A key focus is on how to define
classes, which are the templates for objects themselves.
All of the constructs of structured programming from the previous chapters still apply within object-oriented
programming. However, by wrapping those constructs within classes, you can create larger, more organized
programs that are more maintainable. The transition from structured, control-flow-based programs to object-oriented
programs somewhat revolutionized programming because it provided an extra level of organization. The result was
that smaller programs were simplified somewhat; but more importantly, it was possible to create much larger
programs because the code within those programs was better organized.
One of the key advantages of object-oriented programming is that instead of creating new programs entirely from
scratch, you can assemble a collection of existing objects from prior work, extending the classes with new features,
adding more classes, and then reassembling everything to provide new functionality.
Readers unfamiliar with object-oriented programming should read the Beginner Topic blocks for an introduction.
The general text outside of the Beginner Topics focuses on using C# for object-oriented programming with the
assumption that readers are already familiar with object-oriented methodology.
This chapter delves into how C# supports encapsulation through its support of constructs such as classes, properties,