> 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/console-input-and-output.md).

# Console Input & Output

Learn how to display information in the console and receive input from the user in C#.

## What is Console I/O

**Input and Output (I/O)** is how your program communicates with the user.

* **Output** → Your program displays something to the user.
* **Input** → The user enters something into your program.

C# provides the `Console` class for working with the terminal.

```csharp
Console.WriteLine("Hello World!");
```

Output:

```powershell
Hello World!
```

### Console.WriteLine()

`Console.WriteLine()` writes something to the console and then moves to a **new line**.

```csharp
Console.WriteLine("Hello!");
Console.WriteLine("Welcome to C#.");
```

Output:

```powershell
Hello!
Welcome to C#.
```

You can also print variables:

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

Console.WriteLine(name);
Console.WriteLine(age);
```

Output:

```powershell
John
18
```

### Console.Write()

`Console.Write()` works like `WriteLine()`, except it **doesn't create a new line** afterward.

```csharp
Console.Write("Hello ");
Console.Write("World!");
```

Output:

```powershell
Hello World!
```

#### `Write` vs `WriteLine`

```csharp
Console.Write("A");
Console.Write("B");
Console.Write("C");
```

Output:

```powershell
ABC
```

Compared to:

```csharp
Console.WriteLine("A");
Console.WriteLine("B");
Console.WriteLine("C");
```

Output:

```powershell
A
B
C
```

{% hint style="info" %}
**Hint:** Use `Console.Write()` when you want the user's input to appear on the same line as a question.
{% endhint %}

### Console.ReadLine()

`Console.ReadLine()` waits for the user to type something and press **Enter**.

```csharp
Console.ReadLine();
```

Usually, you'll save the input in a variable:

```csharp
string name = Console.ReadLine();
```

Now whatever the user enters is stored inside `name`.

A more useful example:

```csharp
Console.Write("Enter your name: ");

string name = Console.ReadLine();

Console.WriteLine("Hello " + name);
```

Console:

```powershell
Enter your name: John
Hello John
```

### String Interpolation

Instead of combining strings using `+`:

```csharp
Console.WriteLine("Hello " + name);
```

you can use **string interpolation**:

```csharp
Console.WriteLine($"Hello {name}");
```

The `$` before the string allows you to put variables directly inside `{ }`.

You can use multiple variables:

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

Console.WriteLine($"My name is {name} and I am {age} years old.");
```

Output:

```powershell
My name is John and I am 18 years old.
```

For most cases, string interpolation is easier to read than combining strings with `+`.

### Reading Numbers

There's one important thing to know about `Console.ReadLine()`:

It returns a `string`.

So this won't work:

```csharp
int age = Console.ReadLine(); // ❌ Error
```

Even if the user enters `18`, C# initially sees it as `"18"`.

You need to convert it:

```csharp
Console.Write("Enter your age: ");

int age = int.Parse(Console.ReadLine());

Console.WriteLine($"You are {age} years old.");
```

We'll cover conversions such as `int.Parse()` and `TryParse()` properly in **Type Conversion**.

### Complete Example

Now we can combine variables, input and output:

```csharp
Console.Write("What is your name? ");
string name = Console.ReadLine();

Console.Write("How old are you? ");
int age = int.Parse(Console.ReadLine());

Console.WriteLine();
Console.WriteLine($"Hello {name}!");
Console.WriteLine($"You are {age} years old.");
```

Example:

```powershell
What is your name? John
How old are you? 18

Hello John!
You are 18 years old.
```

## Quick Reference

```csharp
// Output + new line
Console.WriteLine("Hello World!");

// Output without new line
Console.Write("Hello World!");

// Read user input
string input = Console.ReadLine();

// Question + input
Console.Write("Enter your name: ");
string name = Console.ReadLine();

// String interpolation
Console.WriteLine($"Hello {name}!");

// Read a number
int age = int.Parse(Console.ReadLine());
```
