> For the complete documentation index, see [llms.txt](https://docs.fawsstudio.dk/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.fawsstudio.dk/c-cheatsheet/variables-and-data-types.md).

# Variables & Data Types

Learn how to create variables, store values, and use the most common data types in C#.

## Introduction

A **variable** is used to store a value that your program can use or change later.

In C#, every variable has a **data type**, which determines what kind of value it can store.

```c#
string name = "John";
int age = 18;
bool isOnline = true;
```

The basic syntax for declaring a variable is:

```csharp
dataType variableName = value;
```

*For example:*

```csharp
int score = 100;
```

Here, `int` is the **data type**, `score` is the **variable name**, and `100` is the **value**.

## Common Data Types

These are the most common data types you'll use when starting with C#:

| Type      | Description                              | Example                 |
| --------- | ---------------------------------------- | ----------------------- |
| `int`     | Whole numbers                            | `int age = 18;`         |
| `double`  | Decimal numbers                          | `double height = 1.82;` |
| `float`   | Decimal number with less precision       | `float speed = 5.5f;`   |
| `decimal` | High-precision decimal, useful for money | `decimal price=49.95m;` |
| `string`  | Text                                     | `string name ="John";`  |
| `char`    | A signle character                       | `char grdae =``'A';`    |
| `bool`    | `true` or `false`                        | `bool isOnline = true;` |

{% hint style="info" %}
`string` values use double quotes (`"Hello"`), while `char` values use single quotes (`'A'`).
{% endhint %}

|   |
| - |
|   |

### Declaring Variables

You can create a variable and give it a value immediately:

```csharp
int age = 18;
string username = "John"
```

Or declare it first and assign a value later:

```csharp
int score;

score = 100;
```

You can also change the value:

```csharp
int score = 100;

score = 200;

Console.WriteLine(score);
```

Output:

```
200
```

A variable can only contain values compatible with its type:

```csharp
int age = 18;

// ❌ Error
age = "eighteen";
```

### Using var

`var` lets C# automatically determine the variable's data type based on the assigned value.

```csharp
var name = "John";
var age = 18;
var online = true;
```

C# interprets these as:

```csharp
string name = "John";
int age = 18;
bool online = true;
```

`var` does **not** mean that the variable can later change to another type.

```csharp
var age = 18;

// ❌ Error
age = "John";
```

Once C# determines that `age` is an `int`, it stays an `int`.

### Constants

Sometimes you have a value that should **never change**. Use `const` for this.

```csharp
const double Pi = 3.14159;
const int MaxPlayers = 10;
```

Trying to change it later causes an error:

```csharp
const int MaxPlayers = 10;

// ❌ Error
MaxPlayers = 20;
```

### Naming Variables

Variable names should describe what they contain.

Good:

```csharp
string username;
int playerScore;
bool isLoggedIn;
```

Less useful:

```csharp
string firstName;
int playerScore;
bool isLoggedIn;
```

C# commonly uses **camelCase** for local variables:

```csharp
string firstName;
int playerScore;
bool isLoggedIn;
```

{% hint style="info" %}
Variable names are case-sensitive. `score`, `Score`, and `SCORE` are three different names in C#.
{% endhint %}

### Quick Reference

```csharp
// Whole number
int age = 18;

// Decimal
double height = 1.82;

// Text
string name = "John";

// Single character
char grade = 'A';

// True / false
bool isOnline = true;

// Automatically determine type
var score = 100;

// Constant value
const int MaxPlayers = 10;
```

#### Remember

`Console.WriteLine()` → **write + new line**

`Console.Write()` → **write without new line**

`Console.ReadLine()` → **read user input**

`$"Hello {name}"` → **insert variables into text**
