Your First Program
Create a console app with dotnet new console, read the Program.cs it generates, and print a line of text.
dotnet new console -o <Name>A console application is a program with no windows and no buttons. It writes plain text into a terminal and can read plain text back. It is the smallest useful kind of program, so it is where every .NET course starts.
Creating it
Run dotnet new console -o Hello. That creates a folder named Hello holding two files you care about: Hello.csproj, which describes the project, and Program.cs, which holds your code. Then cd Hello and dotnet run. It prints Hello, World!.
Program.cs contains a comment and one statement. A comment starts with // and the compiler ignores it; it is there for people. The statement Console.WriteLine("Hello, World!"); prints one line. The text in double quotes is what gets printed, and the semicolon marks the end of the statement.
The short form and the long form
Those few lines are a complete program because of top-level statements, a shorthand added in C# 9. The long form is what C# always required: an entry point method named Main, inside a type. dotnet new console --use-program-main generates that version instead. Both are valid and both compile to the same program. With top-level statements the compiler writes the type and the entry point for you; the name of the generated method is not Main and your code cannot call it.
What trips people up
Only one file in a project may hold top-level statements, and in that file they must come before any type or namespace declaration.
Example
// Program.cs, as created by: dotnet new console -o Hello
// These statements are the whole program. The compiler wraps them in a
// generated type with a generated entry point method.
Console.WriteLine("Hello, World!");
// Statements run in the order they are written.
Console.WriteLine("The program ends after the last statement.");
// Output:
// Hello, World!
// The program ends after the last statement.When to use it
- Starting a scratch project to try out one piece of C# on its own.
- Reading an older repository where Program.cs still declares a class and a Main method.
- Writing a small command-line tool that takes a file name as an argument.
More examples
The same program with an explicit Main
Reach for this form when a team's rules ask for it, and expect to read it in projects written before .NET 6. Main is the method the runtime calls first; string[] args holds the words typed after the program name on the command line.
// Generated by: dotnet new console -o Hello --use-program-main
// This file has no top-level statements, so the type may come first.
namespace Hello;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
Console.WriteLine("The program ends after the last statement.");
}
}
// Output:
// Hello, World!
// The program ends after the last statement.Create and run, start to finish
This is the whole loop you will repeat for the rest of the course: create, edit Program.cs, run. Use dotnet run while you are still changing the code, because it compiles and starts in one step.
dotnet new console -o Hello
cd Hello
dotnet run
# Hello, World!
# What is in the folder:
# Hello.csproj describes the project
# Program.cs your code
# obj/ working files the build needs; never edit theseReading the command line
The two dashes separate arguments meant for dotnet run from arguments meant for your program. Use args for small tools where a value changes per run, such as a file name to process.
// In a file with top-level statements, args is already available.
// It holds the words typed after the program name, and is never null.
// Run with: dotnet run -- Ada Grace
Console.WriteLine("argument count: " + args.Length);
// Output, when run with: dotnet run -- Ada Grace
// argument count: 2
Discussion