Site

Hello — Your First C# Program

Tutorial 1.0  •  C# / Learn

1.0 What This Teaches

This tutorial introduces the smallest useful C# program. It covers:

1.1 Your First Program

In C# 9 and later, a program can consist of just one statement:
// Program.cs - first C# program.
Console.WriteLine("Hello, World!");
Save this as Program.cs. There is no class, no Main method, and no namespace declaration. The compiler treats the entire file as the program entry point. This is called a top-level statement file. Older C# code required a class and a static void Main(string[] args) method. You will still see that pattern in existing codebases, but new projects use top-level statements.

1.2 Project Structure

Running dotnet new console -n Hello creates two things: A minimal .csproj looks like this:
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net8.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
  </PropertyGroup>
</Project>
ImplicitUsings adds common using directives automatically, which is why you can call Console.WriteLine without writing using System;.

1.3 Console.WriteLine

Console.WriteLine writes to standard output and appends a newline. Console.Write writes without the newline.
Console.WriteLine("Hello, World!");   // prints and moves to next line
Console.Write("Hello ");              // cursor stays on same line
Console.Write("World!\n");            // \n is an explicit newline
String literals use double quotes. The \n inside a string is the escape sequence for a newline character.

1.4 Building and Running

The dotnet CLI handles compiling and running. From the project directory:
dotnet run
This compiles and immediately executes the program. You will see:
Hello, World!
To compile only without running:
dotnet build
The compiled output lands in bin/Debug/net8.0/Hello.exe (Windows) or bin/Debug/net8.0/Hello (Linux/macOS).

1.5 Example - Reading Input

A program that reads a name and prints a personalized greeting:
// Program.cs - greet the user by name.
Console.Write("Enter your name: ");
string? name = Console.ReadLine();
Console.WriteLine($"Hello, {name}!");
Expected interaction:
Enter your name: Alice
Hello, Alice!
Console.ReadLine() reads a line from stdin and returns string? (nullable string). The $"..." prefix marks an interpolated string - the expression inside {} is evaluated and inserted into the output.

1.6 Exercise

Exercise Modify Program.cs so it prints your name and the current year, each on its own line. Use two separate Console.WriteLine calls. Then add input: read the user's name and print a personalized greeting using string interpolation. Build and run to confirm.

1.7 Common Mistakes

Lowercase console

C# is case-sensitive. Writing console.WriteLine instead of Console.WriteLine causes a compile error. The class name starts with an uppercase C.

Missing semicolon

Every C# statement ends with a semicolon. Omitting it produces a compile error, sometimes on the following line rather than the one with the mistake.

Adding "using System;" unnecessarily

Projects with ImplicitUsings enabled already include using System;. Adding it manually is harmless but produces a "redundant using" warning in most editors.

1.8 Key Terms

TermMeaning
ConsoleStatic class providing stdin, stdout, and stderr
Console.WriteLineWrites a string followed by a newline to stdout
Console.ReadLineReads a line of text from stdin; returns string?
top-level statementsC# 9+ feature: file-level code without a class or Main wrapper
.csprojXML project manifest: target framework, settings, package references
dotnet runCLI command that compiles and executes the current project
interpolated stringString prefixed with $ that evaluates expressions inside {}
ImplicitUsingsProject setting that auto-adds common using directives