C# Get Started
Every C# console program starts execution from the Main() method, which lives inside a class. Statements are grouped into methods, and methods are grouped into classes.
A C# program is compiled by the .NET compiler into an intermediate language (IL), which the .NET runtime then executes. This gives C# both safety and good performance.
using System;
class Program {
static void Main() {
// code goes here
}
}Anatomy of a C# program
A basic C# program has a using directive for namespaces, a class definition, and a Main() method as the entry point. Statements inside methods end with a semicolon.
Compiling and running
Use `dotnet run` to compile and execute your project in one step during development, or `dotnet build` to just compile it into a binary.
using System;
class Program {
static void Main() {
Console.WriteLine("My First C# Program");
}
}My First C# ProgramThe using directive brings in Console, and Main() is where execution begins.
using System;
class Program {
static void Main() {
Console.WriteLine("Line 1");
Console.WriteLine("Line 2");
}
}Line 1
Line 2Multiple statements execute in the order they appear.
Key points
- Every C# console program needs a Main() method.
- using directives bring in namespaces like System.
- Statements end with a semicolon.
- `dotnet run` builds and runs your project in one command.
