# Your First C# Program

Write and run your first C# program, first as a single file with dotnet run, then as a project in the terminal and in Visual Studio.

Novice · C# · Updated November 18, 2026

By Gregor Dzierzon

## After this lesson you can

- Run a single C# file with dotnet run
- Create and run a C# console project
- Choose between Console.Write and Console.WriteLine to keep printing on the same line or end the line
- Read a compiler error's file, line, column, code and message, and fix the first error first

## Key takeaways

- `dotnet run hello.cs` builds and runs a single C# file with no project around it: the quickest way to try an idea.
- `dotnet new console -n Name` creates a console project, and `dotnet run` in its folder runs it.
- A project's code is in `Program.cs`, and its `.csproj` file holds settings such as the .NET version.
- Every C# instruction ends with a semicolon `;`. A compiler error names the file, the (line,column), a code such as CS1002 and the problem; fix the first error first.
- `Console.Write` writes text and stays on the same line; `Console.WriteLine` writes it and then moves to the next line.

A C# program is a set of instructions, written in a text file, that the computer follows from top to bottom. The .NET SDK you installed during setup turns those instructions into something your computer can run, and runs it.

In this lesson you will run C# in two ways: a single file, which is the quickest way to try an idea, and a project, which is how real apps and your exercises are built.

## One file, one command

Open Git Bash and make a folder for this chapter's practice:

```bash
mkdir -p ~/bootcamp/csharp-basics
cd ~/bootcamp/csharp-basics
code hello.cs
```

VS Code opens an empty file named `hello.cs`. The `.cs` extension tells .NET that the file holds C# code. Type this line, then save the file with **Ctrl+S**:

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

Back in Git Bash, run it:

```bash
dotnet run hello.cs
```

The first run takes a few seconds while .NET builds your program. Then you should see:

```text
Hello, C#!
```

Here is what that one line does:

- `Console` is the terminal window your program runs in.
- `WriteLine` writes a line of text there.
- The text in double quotes, `"Hello, C#!"`, is what gets written.
- The semicolon `;` ends the instruction. Every C# instruction ends with one.

Change the text, save, and run it again. You can try any idea in this course this way: one file, one command. When a lesson says **Try it**, this is how.

## Write or WriteLine: staying on the line

Build-It Pizzeria's counter screen should show an order as `Order number: 1004`, all on one line. The label and the number come from two different places, so they are two instructions. Try it with `WriteLine` twice, in a new file `order.cs`:

```csharp
Console.WriteLine("Order number: ");
Console.WriteLine("1004");
```

```text
Order number:
1004
```

The number lands on a line of its own. That's because `WriteLine` does two things: it writes the text, then it moves to the start of the next line, the way you press Enter at the end of a line you've typed.

`Console` has a second method that does only the first half. **`Console.Write`** writes the text and stays where it stopped, so whatever is printed next continues on the same line:

```csharp
Console.Write("Order number: ");
Console.WriteLine("1004");
Console.Write("Pizza: ");
Console.Write("Large");
Console.WriteLine(" pepperoni");
```

```text
Order number: 1004
Pizza: Large pepperoni
```

| Method | Writes the text | Then |
|---|---|---|
| `Console.Write("...")` | yes | stays on the same line |
| `Console.WriteLine("...")` | yes | moves to the next line |
| `Console.WriteLine()` | nothing | moves to the next line, so you get an empty line |

`Write` adds no space for you: the space in `"Order number: "` and in `" pepperoni"` is what keeps the words apart. Finish a line with `WriteLine`, so the next thing printed starts on a fresh line.

## When the build fails: reading a compiler error

Sooner or later you'll forget a semicolon. Here is the pizzeria's welcome sign, saved as `welcome.cs`, with the semicolon missing from its first line:

```csharp
Console.WriteLine("Welcome to Build-It Pizzeria!")
Console.WriteLine("Build your own pizza.");
```

Run it with `dotnet run welcome.cs`, and instead of the welcome you get this:

```text
C:\Users\you\bootcamp\csharp-basics\welcome.cs(1,51): error CS1002: ; expected

The build failed. Fix the build errors and run again.
```

Before running anything, .NET's **compiler** reads your code and turns it into something the computer can run. When it finds code it can't make sense of, it stops and prints a **compiler error** instead, and nothing of your program runs. The error looks like noise at first, but it's a precise address, read left to right:

| Part | Here | What it tells you |
|---|---|---|
| The file | `...\welcome.cs` | Which file holds the mistake (the folder before it is your own) |
| (line,column) | `(1,51)` | Line 1, column 51: the 51st character on that line. Here that's right after the closing `)`, where the `;` belongs |
| The code | `CS1002` | The error's number. Every C# compiler error has one, and searching for it finds its explanation |
| The message | `; expected` | What's wrong, in the compiler's words: it expected a semicolon there |

Add the semicolon, save, and run again: the welcome appears.

**One mistake can cause several errors.** This time the closing quote is missing on line 2:

```csharp
Console.WriteLine("Welcome to Build-It Pizzeria!");
Console.WriteLine("Build your own pizza.);
```

```text
C:\Users\you\bootcamp\csharp-basics\welcome.cs(2,19): error CS1010: Newline in constant
C:\Users\you\bootcamp\csharp-basics\welcome.cs(2,43): error CS1026: ) expected
C:\Users\you\bootcamp\csharp-basics\welcome.cs(2,43): error CS1002: ; expected
```

Three errors, one mistake. The first one points at column 19, where the text in quotes starts. A **constant** is the compiler's word for a value written straight into the code, like this text, and *Newline in constant* means the line ended while the text was still open. Because the quote never closed, the compiler reads `.);` as part of the text, so it also misses the `)` and the `;`: those are the other two errors, at column 43, the end of the line.

Add the closing quote after `pizza.`, and all three disappear. That's the habit to build: **read the first error first and fix it, then run again.** The errors after it are often the first one's echo.

In Visual Studio, the same errors appear in the **Error List** window, with the code, the message and the line, and a red squiggle under the spot in the editor; double-click an error to jump to it.

## A C# project

A real app has more than one file, settings, and libraries it uses. A **project** keeps all of that together in one folder. Make one:

```bash
dotnet new console -n FirstProject
cd FirstProject
ls
```

`dotnet new console` creates a console app (a program that runs in the terminal), and `-n FirstProject` names it. You should see:

```text
FirstProject.csproj  obj  Program.cs
```

- `FirstProject.csproj` is the **project file**. It says which version of .NET the project uses and which libraries it needs. You will rarely edit it by hand.
- `Program.cs` holds the code. Open it and you will find one line: `Console.WriteLine("Hello, World!");`
- `obj` is a working folder .NET uses while it builds. You can ignore it.

Run the project from its folder:

```bash
dotnet run
```

```text
Hello, World!
```

The code in `Program.cs` is written as **top-level statements**: instructions at the top of the file, run in order, with nothing around them. That is all a small program needs. Larger programs organize code into classes and methods, which you will meet soon.

## The same project in Visual Studio

Visual Studio builds the same kind of project with a few clicks:

1. Open **Visual Studio 2026** and choose **Create a new project**.
2. Choose **Console App** (the one marked **C#**), then **Next**.
3. Set **Project name** to `FirstApp` and choose a **Location** inside your `bootcamp` folder. Click **Next**.
4. Make sure **Framework** shows **.NET 10.0**. Leave **Do not use top-level statements** unchecked. Click **Create**.
5. Press **Ctrl+F5** (or choose **Debug > Start Without Debugging**).

A terminal window opens and shows `Hello, World!`. Press any key to close it.

Visual Studio and `dotnet` in Git Bash make exactly the same project. Use whichever you like; the exercises in this course work in both.

## Do it: print the pizzeria's welcome sign

Build-It Pizzeria, the pizzeria from the first chapter, wants the computer at its counter to greet customers in the terminal.

The box below holds `Program.cs`, the file `dotnet new console` makes. Right now it has the one line every new project starts with, so it prints `Hello, World!`.

Your program should print exactly these three lines, in this order:

```text
Welcome to Build-It Pizzeria!
Build your own pizza.
Open every day, 11 AM to 10 PM.
```

**Your job:** change `Program.cs` so it prints the three lines above.

- Print each line with its own instruction, in the order shown.
- Copy each line exactly: the same capital letters, spaces and punctuation.
- Print nothing else, so the `Hello, World!` line goes.

Practiced live on the lesson page: write the C# right there and check it.

## Do it: put each label beside its value

The counter screen shows each order, but every label sits on a line of its own, with its value on the line below. The program in the box prints four lines:

```text
Order number:
1004
Pizza:
Large pepperoni
```

The screen should show two lines, each label followed by its value:

```text
Order number: 1004
Pizza: Large pepperoni
```

**Your job:** change the program so it prints exactly those two lines.

- Keep the four instructions, in the same order, each writing the same text.
- Change only which method each instruction calls, so a label and its value share a line.
- Each line ends after its value, so `Pizza:` starts a new line.

Practiced live on the lesson page: write the C# right there and check it.

## Do it: fix the sign that won't build

Someone typed the pizzeria's closing sign in a hurry. The program in the box doesn't build: when you run it, the compiler stops with errors, and nothing is printed. Read the errors, fix the mistakes they point to, and run it again.

When it builds, it should print exactly these three lines:

```text
Thanks for visiting Build-It Pizzeria!
Your pizza is our pleasure.
See you tomorrow at 11 AM.
```

**Your job:** fix the program so it builds and prints those three lines.

- Change only what the compiler complains about: keep the three instructions and their text as they are.
- Fix the first error first, then run again and read what's left.

Practiced live on the lesson page: write the C# right there and check it.

## Check yourself

2 questions to try on the lesson page.

## So what

You can now run C# two ways: a single file with `dotnet run hello.cs` for quick experiments, and a project for anything bigger. Every program in this course starts one of these two ways.

## Key terms

- [C#](https://www.ask-codey.com/glossary/c-sharp)
- [Console.WriteLine()](https://www.ask-codey.com/glossary/console-writeline)
- [Semicolon](https://www.ask-codey.com/glossary/semicolon-c-sharp)
- [dotnet CLI](https://www.ask-codey.com/glossary/dotnet-cli)
- [Project File (.csproj)](https://www.ask-codey.com/glossary/project-file-csproj)
- [Top-Level Statements](https://www.ask-codey.com/glossary/top-level-statements)
- [Compiler](https://www.ask-codey.com/glossary/compiler)

## Cheat sheets

- [Your First C# Program](https://www.ask-codey.com/cheat-sheets/csharp-projects-your-first-csharp-program-cheat-sheet)

Source: https://www.ask-codey.com/lessons/csharp-projects-your-first-csharp-program
