0.4.12 Basic Data Types in C#

C# Requires Values to Have Types

C# is a strongly typed programming language.

That means values, variables, and fields are associated with types that describe what kind of information they can hold.

You have already modeled information such as:

Now you can connect those ideas to basic C# types.

string Stores Text

Use string for text.

Example:

C#
string playerName = "Jordan";

The type is:

C#
string

The variable name is:

C#
playerName

The current value is:

C#
"Jordan"

Quotation marks indicate a string literal.

Other soccer examples include:

C#
string teamName = "Wildcats";
string opponentName = "Rangers";

int Stores Whole Numbers

Use int for whole-number values within its supported range.

Example:

C#
int jerseyNumber = 7;

Other examples:

C#
int goals = 3;
int playerCount = 18;

These values do not require a fractional part.

double Stores Numbers With Fractional Values

A simple introductory type for measurements with a fractional part is double.

Example:

C#
double heightMeters = 1.82;

Another example:

C#
double distanceMeters = 12.5;

A double can represent many fractional numeric values.

Later programming work may introduce more detail about numeric precision and specialized numeric types.

For now, focus on choosing a type that can represent the needed value.

bool Stores true or false

A Boolean type in C# is:

C#
bool

Example:

C#
bool isAvailable = true;

or:

C#
bool hasStarted = false;

The values are written without quotation marks.

This is different from the strings:

C#
"true"
"false"

which are text.

Declarations Combine Type, Name, and Value

Consider:

C#
int jerseyNumber = 7;

You can read it in three parts.

Type

Plain text
int

Name

Plain text
jerseyNumber

Initial value

Plain text
7

Likewise:

C#
bool isAvailable = true;

means:

Create a Boolean variable named isAvailable and give it the value true.

A Variable's Type Controls Which Values Fit

If a variable is declared as:

C#
int goals = 3;

the program expects whole-number int values for that variable.

A text value such as:

Plain text
"three"

does not match the type.

The compiler can detect many type mismatches before the program runs.

That is one way type information supports correctness.

UML Types Can Connect to C# Types

A UML class attribute might show:

Plain text
jerseyNumber : int

A C# class could later contain a corresponding field:

C#
int jerseyNumber;

Likewise:

Plain text
isAvailable : bool

can connect to:

C#
bool isAvailable;

The UML and C# are different representations, but the type meaning can remain aligned.

Choose the Type From the Meaning

A simple soccer model might use:

C#
string playerName = "Jordan";
int jerseyNumber = 7;
double heightMeters = 1.82;
bool isAvailable = true;

Each type matches the kind of information being stored.

Do not choose a type because it makes one example convenient.

Choose it because it represents the information correctly.

This Module Is About Basic Type Recognition

At this point, the goal is not to memorize every type C# supports.

Focus on four common choices:

Those four types are enough to connect the modeling ideas in this module to simple C# information storage.