Previous Section  < Day Day Up >  Next Section

Chapter 3. Class Design in C#

Topics in This Chapter

  • Defining a Class: The attributes and modifiers included in a class definition influence the behavior and accessibility of the class.

  • Constants: A const type defines fixed values at compilation time.

  • Fields: A field is typically used to maintain data inside a class.

  • Properties: A property is the recommended way to expose a class's data.

  • Methods: The functionality of a class is defined by its methods.

  • Inheritance: By using inheritance, a class can take advantage of preexisting types.

  • Constructors: This special purpose method is used to initialize a class.

  • Events and Delegates: A program's actions are often triggered by events; delegates have the role of invoking methods to handle an event.

  • Operator Overloading: Operators can be used to manipulate classes.

  • Interfaces: An inherited interface defines the methods and properties that a struct or class must implement.

  • Generics: By creating a generic class to store data, it is possible to eliminate casting, boxing, and ensure type safety.

  • Structures: In some situations, a struct is a better choice than a class.

This chapter provides an advanced introduction to using classes within the .NET environment. It is not a primer on object-oriented programming (OOP) and assumes you have some familiarity with the principles of encapsulation, inheritance, and polymorphism. C# is rich in object-oriented features, and the first challenge in working with classes is to understand the variety of syntactical contstructs. At the same time, it is necessary to appreciate the interaction between C# and .NET. Not only does the Framework Class Library (FCL) provide thousands of predefined classes, but it also provides a hierarchy of base classes from which all C# classes are derived.

The chapter presents topics in a progressive fashion梕ach section building on the previous. If you are new to C#, you should read from beginning to end; if you're familiar with the concepts and syntax, read sections selectively. The chapter begins by presenting the syntactical construct of a class and then breaks it down in detail. Attributes, modifiers, and members of the class body (constructors, properties, fields, and methods) are all explained. Sprinkled throughout are recommended .NET guidelines and best practices for designing and using custom classes. The objective is to show not only how to use classes, but also encourage good design practices that result in efficient code.

    Previous Section  < Day Day Up >  Next Section