1.2.9 Defining Classes and Fields

A Class Definition Gives C# the Blueprint for a Custom Type

A simple C# class begins with a class declaration and a body.

For example:

C#
class Player
{
}

This defines a custom type named Player.

The class does not create a Player object by itself.

Objects are created later with new Player().

Add Fields Inside the Class

A simple class can define fields:

C#
class Player
{
    public string name;
    public int jerseyNumber;
    public bool isAvailable;
}

Each field has:

The detailed meaning of public and private appears later in this batch.

One Class Definition Supports Many Objects

Once the Player class exists:

C#
Player player1 = new Player();
Player player2 = new Player();

both objects use the same field structure.

Their field values remain separate.

Match the Class to the UML

If the UML class diagram shows:

Plain text
Player
-------------------------
name : string
jerseyNumber : int
isAvailable : bool

the C# class should contain corresponding fields with compatible names and types.

The goal is not to add every possible Player detail.

Implement the structure represented by the current design.

Keep One Clear Responsibility

At this stage, each class should represent one coherent concept.

A Player class should describe Player state.

A Team class should describe Team state.

Do not place unrelated fields into one class simply because it is convenient.