Chapter 4

Holding a Class Responsible

IN THIS CHAPTER

Bullet Protecting a class

Bullet Working with class constructors

Bullet Constructing static or class members

Bullet Working with expression-bodied members

A class must be held responsible for its actions. Just as a microwave oven shouldn’t burst into flames if you press the wrong key, so a class shouldn’t allow itself to roll over and die when presented with incorrect data.

To be held responsible for its actions, a class must ensure that its initial state is correct and then control its subsequent state so that it remains valid. C# provides both these capabilities. This chapter discusses how to make your classes responsible members of the code community. After all, you wouldn’t want to design a renegade class that runs amok and creates chaos.

Remember You don’t have to type the source code for this chapter manually. In fact, using the downloadable source is a lot easier. You can find the source for this chapter in the \CSAIO4D2E\BK02\CH04 folder of the downloadable ...

Get C# 10.0 All-in-One For Dummies now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.