First Program: top-level statements vs Program class; build & run
Compare top-level statements with the classic Program class, then build and run with the dotnet CLI; finish with quick debugging tips.
First Program: top-level statements vs Program class; build & run is a free C# Academy lesson on CoddyKit — lesson 2 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Two entry styles
Goal: See two ways to write your first app: top-level statements and the classic Program class with Main.
- Same behavior, different style
- Pick the style that is simplest for your sample
Build & run
dotnet CLI basics:
- dotnet new console – create project
- dotnet build – compile
- dotnet run – build and run
Top-level sample
Top-level style removes boilerplate; write statements directly and the compiler creates the entry point.
using System;
class Program
{
static void Main()
{
// Hello message
Console.WriteLine("Hello from classic Main method!");
// A tiny calculation
int a = 2;
int b = 5;
Console.WriteLine("Sum = " + (a + b));
// Interpolation example (C# 6 supports string interpolation)
string user = "Ada";
Console.WriteLine($"Welcome, {user}!");
}
}
Program.Main sample
The Program class with Main is explicit and scales well as files grow.
using System;
public class Program
{
// Classic entry point pattern; useful for larger apps.
public static void Main(string[] args)
{
Console.WriteLine("Hello from Program.Main!");
// Small arithmetic
int x = 3;
int y = 4;
Console.WriteLine("Product = " + (x * y));
// Simple branching
int n = 7;
if (n % 2 == 1)
{
Console.WriteLine("n is odd");
}
else
{
Console.WriteLine("n is even");
}
}
}
Project layout & restore
A console app includes .csproj and source files (e.g., Program.cs).
- dotnet restore downloads NuGet deps
- Keep names short and focused
Debugging basics
Debug quickly: set a breakpoint, step over, and inspect locals.
- Use Console.WriteLine for quick probes
- Reproduce small cases to isolate bugs
Top-level benefit
Quick check: What is a key benefit of top-level statements?
Recap
Recap: Use top-level statements for tiny samples, or Program.Main for structure. Build with dotnet build and run with dotnet run.
Frequently asked questions
Is the “First Program: top-level statements vs Program class; build & run” lesson free?
Yes — the full text of “First Program: top-level statements vs Program class; build & run” is free to read here on the web, and the C# Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.
What will I learn in “First Program: top-level statements vs Program class; build & run”?
Compare top-level statements with the classic Program class, then build and run with the dotnet CLI; finish with quick debugging tips. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C# Academy?
No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 3, so you can start here or from the beginning and move at your own pace.
How long does the “First Program: top-level statements vs Program class; build & run” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C# Academy lesson?
Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- What is C#, CLR/.NET, JIT vs AOT, SDK & dotnet CLI
- First Program: top-level statements vs Program class; build & run
- Project layout, NuGet restore, debugging basics