With your development environment set up now, we can delve a little further into programming with C# by covering some of the basic data structures:
- Integers
- Single precision (floats) and Double precision (doubles)
- Characters and Strings
- Booleans
- Enumerations
For a detailed description of every available data type (reference and value) available to you, check out Microsoft’s Developer Network.
C# is what is known as a strongly typed language (that is, you have to tell the compiler what type of data your variables are storing). For this tutorial and on I will attempt to give the explanations from a gaming perspective (that is, how would what we are talking about be used in a video game?).
Integer Values
Integer values store whole numbers, and are declared as follows:
int currentHP = 500;
Integers are invaluable for storing data related to numbers that should never be fractional (number of items in inventory, number of quests remaining, experience points, gold amount etc.)
Floats & Doubles
Floats (32-bit) and doubles (64-bit) can store very large and very small numbers, but are only accurate to a certain number of digits and are declared as follows:
float percentofhealthremaining = 0.5f; double attackSpeed = 0.25;
Floats and doubles should be utilized to store information relating to any data member that might end up as a non-integer value. This could range from a percentage (health remaining, mana remaining) value to calculating a player’s damage per second.
Characters & Strings
Characters and strings store text related data: A character represents… an individual character or symbol (‘A’, ‘d’), while a string represents an array of character values (“Cloud”):
char battleGrade = 'A'; string charName = "Cloud";
String and character values can be used to store the names of virtually everything in a game environment.
Booleans
Booleans store “True” or “False”: That’s it! They are declared as follows:
bool isDead = false; bool isAggressive = true;
Boolean values should be used to store any value that can simply be true or false (is the character dead, in a town, a criminal, etc.)
Enumerations
Last but not least (and my personal favorite): Enumerations. Enumerations consist of a series of named values called the enumerator list. Enumerations are initialized as follows:
enum PlayerClass { Mage, Warrior, Thief, Rogue };
enum MonsterState { Attacking, Dead, WaitingForPlayerInteraction };
Enumerations can add an incredible amount of readability into your code if they are used intelligently.
Next time we will cover basic program flow, and how to control it.