Skip to main content

📝 Lesson 1.3: Your First C# Program

Time to write and run real code. You'll create a project, understand every line of the starter program, run it, and then make it your own.

🎯 Learning Objectives

By the end of this lesson, you will be able to:

  • Create a new C# console project with the dotnet command (and in Visual Studio)
  • Identify the key files in a project and explain what each is for
  • Explain every line of a simple C# program
  • Build and run a program, and change what it does

Estimated Time: 45 minutes

Project: Create, run, and customize a "Hello, World!" console app.

In This Lesson

The "Hello, World!" Tradition

For decades, the very first program people write in a new language does one simple thing: it displays the message "Hello, World!" on the screen. It's a tiny program, but it proves something important — that your tools work and you can go from writing code to running code.

That round trip is the heartbeat of programming:

graph LR A["Write code"] --> B["Build
(compile)"] B --> C["Run"] C --> D["See output"] D -->|"change something"| A style A fill:#eff6ff,stroke:#3b82f6,stroke-width:2px style D fill:#e8f5e9,stroke:#4CAF50,stroke-width:2px

Let's make that loop real. Open a terminal (or VS Code's built-in terminal) and, if you made the csharp-course folder in the last lesson, move into it:

cd csharp-course

Creating a Project

A C# program lives inside a project — a folder with your code plus a small file describing how to build it. The dotnet command can scaffold one for you.

Using the terminal (VS Code and everyone)

Run this command to create a new console project (a program that runs in a text window) named HelloWorld:

dotnet new console -o HelloWorld

Let's decode that command:

  • dotnet new — "create a new project."
  • console — the template to use: a console application.
  • -o HelloWorld — "output it into a new folder named HelloWorld." (-o is short for "output.")

Now move into the new project folder and open it in your editor:

cd HelloWorld
code .

💡 What is code .?

The command code . opens VS Code in the current folder (the . means "here"). If it doesn't work, just open VS Code manually and use File → Open Folder to open the HelloWorld folder.

Using Visual Studio (Windows)

🪟 The Visual Studio way

  1. Open Visual Studio and click Create a new project.
  2. Search for and select Console App (make sure it's the C# one), then click Next.
  3. Name it HelloWorld, choose a location, and click Next, then Create.

Visual Studio creates the same kind of project the dotnet new console command does — just through a wizard instead of the terminal.

Anatomy of a Project

Look inside the HelloWorld folder. Ignoring the auto-generated bin and obj folders (temporary build output), you'll see two important files:

File What it is
Program.cs Your actual C# code. The .cs extension means "C# source." This is the file you'll edit.
HelloWorld.csproj The project file. It tells the SDK how to build the program — which .NET version to target and what kind of output to produce. You rarely edit it by hand at this stage.

⚠️ Leave bin and obj alone

The bin and obj folders are generated automatically when you build. You never edit them, and it's safe to delete them (they'll be recreated). Don't put your own code there.

Open Program.cs. Depending on your .NET version, it will look something like this:

// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");

That's a complete, runnable program! Modern C# uses top-level statements, which let a simple program be just the lines that do the work — no extra scaffolding required. (We'll see the older, longer form in a moment, because you'll encounter it in other people's code.)

Building and Running It

From inside the HelloWorld folder, run:

dotnet run

The dotnet run command does two jobs at once: it builds (compiles) your code and then runs the result. After a moment you'll see:

Output:

Hello, World!

🎉 That's it — you just wrote and ran a C# program!

🪟 In Visual Studio

Instead of typing dotnet run, press the green ▶ Start button (or press F5). A console window pops up showing the same output. You may see "Press any key to close this window" — that's Visual Studio keeping the window open so you can read it.

💡 Two commands you'll use a lot

  • dotnet build — compiles the code and reports any errors, but doesn't run it.
  • dotnet run — builds and runs. This is the one you'll use most while learning.

A Line-by-Line Code Tour

Let's understand exactly what you ran. Here's the program again:

// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");

Line 1 — the comment

Anything after // on a line is a comment: a note for humans that the compiler completely ignores. Comments explain why code does something. You can safely delete this line.

Line 2 — the statement

This single line is doing several things. Let's break it into pieces:

Piece Meaning
Console A ready-made helper (from .NET's class libraries) that represents the text window — the console.
.WriteLine(...) An action you're asking Console to perform: "write a line of text, then move to the next line." The dot . means "belonging to."
("Hello, World!") The input to that action, inside parentheses. Here it's the text to print.
"Hello, World!" A string — text wrapped in double quotes. The quotes mark where the text starts and ends.
; A semicolon ends the statement, like a period ends a sentence. C# requires it.
💡 Read it out loud: "Console, write this line: Hello, World!" That's genuinely all it says.

The longer, "traditional" form

You'll often see beginner programs written in this fuller style. It does the exact same thing as the two-line version — the top-level version just hides this scaffolding for you:

using System;

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, World!");
        }
    }
}

Don't worry about memorizing this now — we'll unpack namespace, class, and Main in later modules. For now, just recognize that both forms are the same program, and that Main is the official "starting point" where a program begins running.

✅ Good to know

Every program needs a starting point. With top-level statements, C# treats your file's top-level lines as the body of Main automatically. Same idea, less typing.

Make It Your Own

The best way to learn is to change things and see what happens. In Program.cs, replace the code with this:

Console.WriteLine("Hello, World!");
Console.WriteLine("My name is Ada.");
Console.WriteLine("I am learning C#!");

Save the file, then run it again:

dotnet run

Output:

Hello, World!
My name is Ada.
I am learning C#!

Notice that each Console.WriteLine prints on its own line, and the program runs the statements top to bottom, in order. That ordering is fundamental: a program is a sequence of steps, carried out one after another.

⚠️ Try breaking it (on purpose!)

Delete the semicolon ; at the end of a line and run again. You'll get a build error mentioning a missing ;. This is the compiler catching your mistake before the program runs — a helpful safety net, not a scolding. Put the semicolon back and it works again.

💡 WriteLine vs. Write

Console.WriteLine prints text and then moves to a new line. Console.Write prints text and stays on the same line. Try swapping one and see the difference in the output.

Exercise & Quiz

🏋️ Exercise: Your Personal Intro Card

Objective: Create your own program from scratch and make it print a small "intro card" about you.

Instructions:

  1. Create a brand-new project called AboutMe (hint: dotnet new console -o AboutMe, then cd AboutMe).
  2. Edit Program.cs so it prints at least four lines: your name, your favorite hobby, why you're learning C#, and one goal you have.
  3. Run it with dotnet run and confirm all four lines appear.
  4. Bonus: Add a "border" line of dashes above and below using Console.WriteLine("--------------------");.

Starter Code:

// TODO: Make this print four lines about you
Console.WriteLine("--------------------");
Console.WriteLine("Name: ");
// Add more lines here...
Console.WriteLine("--------------------");
💡 Hint

Each fact is its own Console.WriteLine("..."); line. Just copy the pattern and change the text inside the quotes. Remember the semicolon at the end of every statement!

✅ Solution
Console.WriteLine("--------------------");
Console.WriteLine("Name: Ada Lovelace");
Console.WriteLine("Hobby: Playing the piano");
Console.WriteLine("Learning C# to: build my own apps");
Console.WriteLine("Goal: Finish this course!");
Console.WriteLine("--------------------");

Running dotnet run produces:

--------------------
Name: Ada Lovelace
Hobby: Playing the piano
Learning C# to: build my own apps
Goal: Finish this course!
--------------------

🎯 Quick Quiz

Question 1: What does the command dotnet run do?

Question 2: What is the purpose of the semicolon ; in C#?

Question 3: What's the difference between Console.WriteLine and Console.Write?

Summary

🎉 Key Takeaways

  • C# code lives in a project; create one with dotnet new console -o Name (or Visual Studio's Create a new project).
  • The two files that matter are Program.cs (your code) and the .csproj (build settings). Ignore bin and obj.
  • dotnet run builds and runs; dotnet build only builds.
  • Console.WriteLine("...") prints a line of text; every statement ends with a ;.
  • Programs run top to bottom, one statement at a time. Modern "top-level statements" are the same as the traditional Main method, with less typing.

📚 Additional Resources

🚀 What's Next?

You can now create, run, and edit programs — that completes Module 1! In Module 2, we start with the real building blocks of programming. First up, Lesson 2.1: Variables and Data Types, where your programs start to remember and work with information.

🎉 You're officially a programmer!

You wrote code, ran it, and changed it. That's the whole game — everything from here builds on this loop.