Skip to main content

How to Create a .NET C# Console App with dotnet new console

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

A console app is the simplest kind of .NET C# program: it runs in the terminal, prints to it and reads from it. dotnet new console creates one in a second, with a project file and a Program.cs that already prints Hello, World!, and dotnet run runs it.

Syntax​

Syntax
dotnet new console -o <folder> [<options>]
dotnet run
PartWhat it does
dotnet new consoleCreates a project from the Console App template.
-o <folder>Creates the project in a new folder with that name. The project gets the folder's name too.
<options>Optional. --use-program-main writes a Program class with a Main method instead of top-level statements, and -f net10.0 picks the .NET version, which is also the default.
dotnet runBuilds the project in the folder you're in and runs it.

The Example​

The example creates a console app named hello-console and runs it, unchanged, to see what the template gives you.

Prerequisites​

  • The .NET 10 SDK. Check it with dotnet --version. This guide uses 10.0.404.

Step 1: Create the App​

Terminal
dotnet new console -o hello-console
Output
An issue was encountered verifying workloads. For more information, run "dotnet workload update".
The template "Console App" was created successfully.

Processing post-creation actions...
Restoring /Users/Shared/you/hello-console/hello-console.csproj:
Restore succeeded.

The first line appears only on the first dotnet command under a user account, and it's harmless. Fixed: An issue was encountered verifying workloads explains it. "Restore" downloads the packages the project needs, which for a new console app are none beyond .NET itself.

This command and the next ones are the same in zsh, bash and PowerShell.

Step 2: Look at What It Created​

The hello-console folder holds two files you'll edit, plus an obj folder that the build uses. Program.cs is the whole program:

Program.cs from the console template: one line, Console.WriteLine with Hello, World!

Program.cs
Console.WriteLine("Hello, World!");

It uses top-level statements: code written straight into the file, which .NET runs from the top, without a class or a Main method around it.

hello-console.csproj describes the project:

hello-console.csproj
<Project Sdk="Microsoft.NET.Sdk">

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

</Project>
SettingWhat it means
OutputType ExeThe project builds a program you can run, not a library.
TargetFramework net10.0It runs on .NET 10.
RootNamespace hello_consoleThe namespace for new classes. The - in the folder's name became _, because a namespace can't contain -.
ImplicitUsingsCommon namespaces, such as System, are imported for you, so Console works without a using line.
NullableThe compiler warns when a value that may be null is used as if it can't be.

Step 3: Run It​

Terminal
cd hello-console
dotnet run
Output
Hello, World!

dotnet run built the project and ran it. The first run takes a few seconds longer while it builds.

A zsh terminal where dotnet new console -o hello-console creates and restores the project, cd moves into it and dotnet run prints Hello, World!

Common Mistakes​

MistakeWhat happensDo this instead
Running dotnet run outside the project folderdotnet can't find a project to runcd into the folder with the .csproj
Leaving out -oThe project is created in the folder you're in, named after itGive it its own folder with -o
Naming the folder with spacesCommands need quotes around every path to itUse a name such as hello-console

Troubleshooting​

SymptomLikely causeFix
An issue was encountered verifying workloads.The first dotnet command under this user accountIt's harmless. See the fixes post linked in Step 1
Couldn't find a project to run.You're not in the project's foldercd hello-console

FAQ​

How do I get a Main method instead of top-level statements?​

Create the project with dotnet new console -o hello-console --use-program-main. The template then writes a Program class with a static void Main(string[] args) that does the same thing.

Can I run a single C# file without a project?​

Yes, since .NET 10. How to Run a Single C# File with dotnet run shows file-based apps.

Conclusion​

dotnet new console -o <folder> creates a console app with a one-line Program.cs and a project file for .NET 10, and dotnet run in that folder builds and runs it.

Additional Resources​