/commands/dotnet-cli/dotnet-new-console
dotnet new console: create and run a C# console app
- tool
- .NET CLI
- versions
- .NET 6 – 10
- os
- linux, macos, windows
dotnet new console -o MyApp creates a folder MyApp with a C# console project (MyApp.csproj and a Program.cs that uses top-level statements). dotnet run --project MyApp, or cd MyApp && dotnet run, restores, builds and runs it. Arguments after -- go to the program.
Create and run
export DOTNET_NOLOGO=1 DOTNET_CLI_TELEMETRY_OPTOUT=1
dotnet new console -o MyApp >/dev/null 2>&1
ls MyApp
sed '1s/^\xEF\xBB\xBF//' MyApp/Program.cs # strip the UTF-8 BOM the template writes
cat > MyApp/Program.cs <<'EOF'
Console.WriteLine($"Hello, {(args.Length > 0 ? args[0] : "World")}!");
EOF
dotnet run --project MyApp -- errcodexOutput
MyApp.csproj
Program.cs
obj
Console.WriteLine("Hello, World!");
Hello, errcodex!Program.cs contains only statements: since C# 9 and .NET 6 the template omits the Main method and using lines (implicit usings).
The project file
export DOTNET_NOLOGO=1 DOTNET_CLI_TELEMETRY_OPTOUT=1
dotnet new console -o MyApp >/dev/null 2>&1
sed '1s/^\xEF\xBB\xBF//' MyApp/MyApp.csprojOutput
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>| Property | Meaning |
|---|---|
| OutputType Exe | Build an executable, not a library; this is what makes dotnet run possible |
| TargetFramework | The .NET version the app runs on; defaults to the SDK version that created it |
| ImplicitUsings | Adds global using directives for common namespaces such as System and System.IO |
| Nullable | Turns on nullable reference type warnings |
With --use-program-main, the template writes a file-scoped namespace and a Program class with static void Main(string[] args) instead of top-level statements.
Useful options
| Option | Effect |
|---|---|
| -o, --output DIR | Folder to create; also the default project name |
| -n, --name NAME | Project name (default: the output folder name) |
| -f, --framework net8.0 | Target framework (an installed SDK must support it) |
| --use-program-main | Generate a classic Program class with static void Main |
| -lang "F#" | Create the F# variant of the template |
| --dry-run | Show which files would be created |
Other common templates
| Command | Creates |
|---|---|
| dotnet new list | Every installed template |
| dotnet new classlib -o Lib | Class library |
| dotnet new webapi -o Api | ASP.NET Core Web API |
| dotnet new xunit -o Tests | xUnit test project |
| dotnet new sln | Solution file; add projects with dotnet sln add |
| dotnet new gitignore | .gitignore for .NET projects |
From template to a shareable program
| Command | Result |
|---|---|
| dotnet run | Restore, build in Debug and run; for development |
| dotnet build -c Release | Compile into bin/Release/<tfm>/; run the DLL with dotnet MyApp.dll |
| dotnet publish -c Release | Copy everything needed to run into a publish folder; needs .NET on the target |
| dotnet publish -c Release -r linux-x64 --self-contained | Include the .NET runtime, so the target needs nothing installed |
Common first errors: dotnet build in a folder with several project or solution files fails with MSB1011 (for example after dotnet new sln next to the project), and a -f value newer than the installed SDK fails with NETSDK1045.
FAQ
Where is the Main method in a new console app?
The compiler generates it. Program.cs uses top-level statements, so the code in the file is the body of an implicit Main, and args is available as a variable. Pass --use-program-main to get an explicit Program class instead.
Which .NET version does dotnet new console target?
The newest framework of the SDK that runs the command, written to TargetFramework in the .csproj (net10.0 with the .NET 10 SDK). Use -f net8.0 to target an older installed framework, or a global.json file to select an older SDK.
How do I build an executable to share?
Use dotnet publish -c Release. Add -r with a runtime identifier such as linux-x64 or win-x64 and --self-contained to include the runtime, so the target machine does not need .NET installed.