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.
Use string for text.
Example:
string playerName = "Jordan";The type is:
stringThe variable name is:
playerNameThe current value is:
"Jordan"Quotation marks indicate a string literal.
Other soccer examples include:
string teamName = "Wildcats";
string opponentName = "Rangers";Use int for whole-number values within its supported range.
Example:
int jerseyNumber = 7;Other examples:
int goals = 3;
int playerCount = 18;These values do not require a fractional part.
A simple introductory type for measurements with a fractional part is double.
Example:
double heightMeters = 1.82;Another example:
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.
A Boolean type in C# is:
boolExample:
bool isAvailable = true;or:
bool hasStarted = false;The values are written without quotation marks.
This is different from the strings:
"true"
"false"which are text.
Consider:
int jerseyNumber = 7;You can read it in three parts.
Type
intName
jerseyNumberInitial value
7Likewise:
bool isAvailable = true;means:
If a variable is declared as:
int goals = 3;the program expects whole-number int values for that variable.
A text value such as:
"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.
A UML class attribute might show:
jerseyNumber : intA C# class could later contain a corresponding field:
int jerseyNumber;Likewise:
isAvailable : boolcan connect to:
bool isAvailable;The UML and C# are different representations, but the type meaning can remain aligned.
A simple soccer model might use:
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.
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.