1.2.7 Custom Types vs. Primitive Types

Video: Primitive vs. Custom Types (10:50)
Programs Use Different Kinds of Types

C# programs work with simple built-in value types and with custom classes.

For introductory reasoning, compare:

int jerseyNumber;
bool isAvailable;
double distanceMeters;

with:

Player player1;
Team team1;

The first group represents basic values.

The second group represents object references to custom types.

Basic Types Represent Individual Values

Examples include:

Activity diagram showing int whole-number value, then double numeric value with fractional capability, then bool true false value.
IntWholeNumberValue.
  • New
  • Changed
  • Removed

A field such as:

int jerseyNumber;

is designed to hold one whole-number value.

A Custom Class Can Represent a Structured Concept

A Player object can contain several related fields.

Conceptually:

Player
- name
- jerseyNumber
- isAvailable
- team

That is more structured than one integer or Boolean.

The class gives the program a type for the complete Player concept.

string Is Commonly Used Like a Basic Type but Is a Reference Type

You have used:

string playerName = "Jordan";

For beginner code, string often feels similar to the basic value types because you assign and display text directly.

Technically, string is a reference type in C#.

You do not need advanced memory details here.

The useful distinction is that custom classes such as Player also produce object references and can contain their own structured state.

Type Compatibility Still Matters

A field declared as:

int jerseyNumber;

expects an integer-compatible value.

A field declared as:

Team team;

expects a compatible Team reference.

The compiler uses type information to prevent many mismatched assignments.

Choose a Type That Matches the Information

Ask:

For:

jersey number

an int may fit.

For:

player

a custom Player class can fit.

The type should communicate the meaning of the information.

Keep the Distinction Useful

Do not reduce the topic to:

A better idea is:

built-in/basic value types represent common individual values;

custom class types let the application represent concepts specific to its domain.