35. Classes#

We’ve discussed the concept of objects - instances of classes that bundle state and behavior. But where do objects come from? In a class-based language such as C#, they come from classes.

A class defines a new data type by specifying a blueprint for creating objects. The objects are the values of that type, or members of the set if you will.

This new data type defines the shape of its objects, in terms of the data they can hold (their state), and the actions they can perform (their behaviors). This two things are known as the ‘members’ of the class.

../_images/cover-classes.jpg

Fig. 35.1 A class is like a ‘blueprint’ for the shape of its objects, in terms of the data they can hold (their state), and the actions they can perform (their behaviors).#

The most common types of members are:

Members can either be, so called ‘instance members’ or they can be ‘static members’. The idea of object oriented programming is tie prefer instance members. We will discuss all the above as instance members in the coming chapters and in a later chapter we will also discuss static members.

To understand the difference between classes and objects think of the difference between the notion of a number and that of any particular number. The idea of a number refers to the set of all numbers. But when talking about any particular number we’re talking about a particular member of the set.

Remember the DateTime class we used in the previous chapter? That’s a class, and when we do new DateTime(2030, 1, 1), we are creating a new instance of the DateTime class. The class DateTime is the idea of a date and a time, while an instance of the DateTime class is an actual and specific date and time.

Let’s write a simple Car class. The Car class may define fields such as Color and Model, and instance methods such as Start and Stop. These properties and methods represent the state and behavior of the Car objects.

In Visual Studio, you can create a new class by following these steps:

  1. Right-click on your project in the Solution Explorer.

  2. Select Add > Class....

  3. In the Add New Item dialog box, enter the name of your class (e.g., Car) in the Name field.

  4. Click Add.

Note

Class names are usually nouns, while method names are usually verbs.

In C#, classes are defined using the class keyword, followed by the name of the class (which should follow PascalCase naming conventions). Here’s a basic Car class that you can use:

public class Car
{
    public string Color = "Unknown";
    public string Model = "Unknown";

    public void Start()
    {
        Console.WriteLine($"Starting {Color} {Model}.");
    }

    public void Stop()
    {
        Console.WriteLine($"Stopping {Color} {Model}.");
    }
}

The name, or identifier, of the class, and hence the data type, is Car. Everything between the first and last curly bracket ({ ... }) is the ‘body’ of the class. This is where we define its members. The keyword public, appearing before the class name and its members is an ‘access modifier’ and we’ll explain what that is very soon.

Once you’ve defined the class, you can use it to instantiate any number of objects in (for example) your Main method (Program.cs) like this:

Car car1 = new Car(); // Creates an object (i.e. a value) of type Car.
Car car2 = new Car(); // Creates an object (i.e. a value) of type Car.

These two variables are pointing to objects of type Car. They are instances of the idea of a car. The class is the idea, the blueprint, and objects are embodiments of that idea.

Note

The code that is generated for you by Visual Studio might contain a namespace. For the sake of simplicity we’ve removed the namespaces in our class examples. If you keep the namespace, remember that if you want to use the type somewhere else then that place has to either be within the same namespace, or you have to qualify the type (like MyNamespace.Car), or import the namespace through the using directive (like using MyNamespace;). Refer to the chapter on namespaces if you need a reminder.

Since classes define reference types, these two objects are not the same, even though they currently seem identical. So car1 == car2 will evaluate to false. It is possible to ‘override’ the behavior of the equals operator (==) so that the above would return true, but we’ll get to that much later.

You can also set and get the values of the public fields of these objects. Let’s set some values so that we can tell the objects apart:

car1.Color = "Red";
car2.Color = "Yellow";

car1.Model = "Tesla";
car2.Model = "Land Rover Defender";

You can of course also invoke any of the public methods that these objects expose.

car1.Start();
car2.Start();
Starting Red Tesla.
Starting Yellow Land Rover Defender.

You should now have some understanding of the difference between classes and objects. Don’t worry if it feels like we’re moving to fast. We will talk about classes a lot more in the coming chapters.