1.0 What This Teaches
- Top-level statements - the modern way to write a minimal C# program
Console.WriteLinefor writing to the console- Project structure created by
dotnet new console - How to build and run with the dotnet CLI
1.1 Your First Program
// Program.cs - first C# program.
Console.WriteLine("Hello, World!");
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.
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
dotnet new console -n Hello creates two things:
Hello.csproj- the project manifest: target framework, compiler settingsProgram.cs- the source file where execution begins
.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
\n inside a string is the
escape sequence for a newline character.
1.4 Building and Running
dotnet CLI handles compiling and running. From the project directory:dotnet run
Hello, World!
dotnet build
bin/Debug/net8.0/Hello.exe (Windows) or
bin/Debug/net8.0/Hello (Linux/macOS).
1.5 Example - Reading Input
// Program.cs - greet the user by name.
Console.Write("Enter your name: ");
string? name = Console.ReadLine();
Console.WriteLine($"Hello, {name}!");
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
console.WriteLine instead of
Console.WriteLine causes a compile error. The class name starts
with an uppercase C.
Missing semicolon
Adding "using System;" unnecessarily
ImplicitUsings enabled already include using System;.
Adding it manually is harmless but produces a "redundant using" warning in most editors.
1.8 Key Terms
| Term | Meaning |
|---|---|
| Console | Static class providing stdin, stdout, and stderr |
| Console.WriteLine | Writes a string followed by a newline to stdout |
| Console.ReadLine | Reads a line of text from stdin; returns string? |
| top-level statements | C# 9+ feature: file-level code without a class or Main wrapper |
| .csproj | XML project manifest: target framework, settings, package references |
| dotnet run | CLI command that compiles and executes the current project |
| interpolated string | String prefixed with $ that evaluates expressions inside {} |
| ImplicitUsings | Project setting that auto-adds common using directives |