0
0
CsharpHow-ToBeginner · 3 min read

How to Create a C# Project from Command Line Quickly

To create a C# project from the command line, use the dotnet new command followed by the project type, like console. For example, dotnet new console -o MyApp creates a new console app in the MyApp folder.
📐

Syntax

The basic syntax to create a C# project from the command line is:

  • dotnet new <template> -o <folder-name>

Here:

  • dotnet new is the command to create a new project.
  • <template> is the type of project, like console, classlib, or webapi.
  • -o <folder-name> specifies the output folder where the project files will be created.
bash
dotnet new console -o MyApp
💻

Example

This example creates a simple C# console application named MyApp. It sets up all necessary files so you can start coding right away.

bash
dotnet new console -o MyApp
cd MyApp
dotnet run
Output
Hello, World!
⚠️

Common Pitfalls

Some common mistakes when creating a C# project from the command line include:

  • Not having the .NET SDK installed or not added to your system PATH.
  • Using an incorrect template name (e.g., consoleapp instead of console).
  • Forgetting to navigate into the project folder before running commands like dotnet run.

Always check your .NET SDK installation by running dotnet --version before creating projects.

bash
REM Wrong template name example
REM dotnet new consoleapp -o MyApp  # This will fail

REM Correct template name example
REM dotnet new console -o MyApp  # This works
📊

Quick Reference

Here are some common project templates you can create with dotnet new:

TemplateDescription
consoleConsole application
classlibClass library project
webapiASP.NET Core Web API project
mvcASP.NET Core MVC web app
workerBackground service worker

Key Takeaways

Use dotnet new <template> -o <folder> to create a new C# project from the command line.
Common templates include console for apps and classlib for libraries.
Make sure the .NET SDK is installed and accessible via your system PATH.
Navigate into the project folder before running or building your project.
Check your .NET SDK version with dotnet --version if you face issues.