1.2.2 Classes as Custom Types

A Class Creates a Type for the Program

C# already provides basic types such as:

C#
int
double
bool
string

A class lets a programmer define a custom type that represents a concept needed by the application.

For a soccer program, useful custom types might include:

Plain text
Player
Team
Match

These names represent domain concepts rather than built-in numeric or text categories.

Use the Custom Type When Declaring Object References

If Player is a class, C# can use Player as a type:

C#
Player player1 = new Player();

Read:

Plain text
Player

as:

this variable/reference is intended to work with a Player object

The class definition gives the custom type meaning.

Custom Types Represent Related Information Together

A Player concept may need:

Plain text
name
jerseyNumber
isAvailable
team

Instead of treating those values as unrelated pieces of data, the Player type groups them under one software concept.

An object of that type can then carry a coherent Player state.

The Type Is Not the Object

Player is the type.

player1 is a reference name.

new Player() creates an object instance.

Keep the three roles distinct:

Plain text
Player        → custom type
player1       → reference name
new Player()  → object creation

One Type Can Produce Many Instances

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

Both objects share the same custom type.

Their field values can differ.

A class lets the program define common structure once and reuse it across many instances.

Custom Types Make Code Match the Domain

Compare:

C#
string value1;
string value2;

with:

C#
Player player1;
Team team1;

The custom types communicate more about the roles of the values.

They make the program's structure easier to relate to the system being modeled.

UML Class Diagrams Also Use Type Thinking

A UML class box named:

Plain text
Player

represents the custom type.

An object diagram header:

Plain text
player1 : Player

means that player1 is an instance of that type.

The same type concept connects UML and C#.