Variables and types
- Game Development course
- Beginner
- Lesson 28/83
- 4 min
Learn basic C# types like int, float and string.
Numeric Types: int and float
In C#, the int type is used to store whole numbers (such as health points, ammo count, level). For decimal and fractional values, the float type is used (such as movement speed, coordinates, time). When defining a float literal in C#, the f suffix must always be attached.
int ammo = 30;
float speed = 7.5f;Decimal numbers without the trailing f are treated as double by default in C# and cause compilation errors.
Text and Logic Types: string and bool
The string type is used to store character sequences and text, with values enclosed in double quotes. To hold logical true/false conditions, the bool type is used. A bool variable can only hold either true or false.
string playerName = "Hero";
bool isAlive = true;In conditional statements and game state checks, bool variables are indispensable.
Practice Declare basic variables
Inside the Main method, declare: hero with string "Savasci", level with int 5, speed with float 4.5f, and isAlive with bool true. Then print them with Console.WriteLine(hero + " - Seviye: " + level + " - Hiz: " + speed + " - Hayatta: " + isAlive);.
Starter code
using System;
public class Program
{
public static void Main()
{
// Degiskenleri buraya tanimla
}
}Sample solution
using System;
public class Program
{
public static void Main()
{
string hero = "Savasci";
int level = 5;
float speed = 4.5f;
bool isAlive = true;
Console.WriteLine(hero + " - Seviye: " + level + " - Hiz: " + speed + " - Hayatta: " + isAlive);
}
}Expected output
Savasci - Seviye: 5 - Hiz: 4.5 - Hayatta: True
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Which suffix must be appended when writing a decimal float value in C# (e.g. 5.5)?
- s
- m
- f Correct answer
- d
Explanation: The C# compiler defaults decimal numbers to double; for float types, 'f' or 'F' must be appended to the number.
2 float literal suffix
float speed = 4.5____;
float speed = 4.5f;
Explanation: In C#, decimal literals of type float must be suffixed with 'f'.
3 Basic C# data types
| Term | Meaning |
|---|---|
int |
Stores whole integer values (e.g. 10) |
float |
Stores floating-point numbers (3.14f) |
string |
Stores text in double quotes ("Unity") |
bool |
Stores true or false boolean value |