Skip to content

/commands/dotnet-cli/dotnet-new-console

dotnet new console: create and run a C# console app

verified onenvironmentlocal · windows 10.0.26200 arm64 · GNU bash 5.3.15 (Git Bash) · 10.0.401updated
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

bash
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 -- errcodex

Output

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

bash
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.csproj

Output

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net10.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

</Project>
PropertyMeaning
OutputType ExeBuild an executable, not a library; this is what makes dotnet run possible
TargetFrameworkThe .NET version the app runs on; defaults to the SDK version that created it
ImplicitUsingsAdds global using directives for common namespaces such as System and System.IO
NullableTurns 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

OptionEffect
-o, --output DIRFolder to create; also the default project name
-n, --name NAMEProject name (default: the output folder name)
-f, --framework net8.0Target framework (an installed SDK must support it)
--use-program-mainGenerate a classic Program class with static void Main
-lang "F#"Create the F# variant of the template
--dry-runShow which files would be created

Other common templates

CommandCreates
dotnet new listEvery installed template
dotnet new classlib -o LibClass library
dotnet new webapi -o ApiASP.NET Core Web API
dotnet new xunit -o TestsxUnit test project
dotnet new slnSolution file; add projects with dotnet sln add
dotnet new gitignore.gitignore for .NET projects

From template to a shareable program

CommandResult
dotnet runRestore, build in Debug and run; for development
dotnet build -c ReleaseCompile into bin/Release/<tfm>/; run the DLL with dotnet MyApp.dll
dotnet publish -c ReleaseCopy everything needed to run into a publish folder; needs .NET on the target
dotnet publish -c Release -r linux-x64 --self-containedInclude 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.

Sources