Welcome to another part of the series C# From Scratch, a course dedicated to teaching you everything you need to know to be productive with the C# programming language.
In the previous part of the series, we learned how to interact with .NET on our machines using the .NET CLI, a tool that was installed as part of the installation of the .NET SDK. That part of the series is available here.
In this part of the series, we’ll create our first C# project using the .NET CLI.
Creating the Project
Use the command ‘dotnet new’ to create a new .NET project.
The command returns with some information about the different options that can be used to create a new .NET project.
The most important options here are the template options that are available. To create a new .NET project, you have to tell .NET what project template you want to base your project on.
This template is used by .NET to create the basic structure of your project. You can see that there are many different templates available to create many different types of applications.
An application that we run from the Command Prompt window is called a Console Application in .NET. You can see from the information returned by dotnet new, that to create a Console Application, we have to use the option ‘console’.
We’ll be working on building a Console Application throughout the rest of this series, so we’ll use the ‘dotnet new console’ command to create a Console Application.
Before we create the application though, let’s create a directory for the project to live in. In the Command Prompt window, use the ‘mkdir’ command to create a new directory called Gradebook.
Now navigate into the new directory using the ‘cd’ (change directory) command.
You can see that you have successfully navigated into the Gradebook directory because the Command Prompt prompt has updated to reflect our new location in the file system.
Finally, you can list the contents of the directory by using the ‘dir’ command. You should see that the new directory is completely empty.
We could create our .NET project here, but instead we’ll try to develop the conventional structure of .NET projects that you will see on larger applications.
Use the mkdir command to create two more directories in the Gradebook directory. The first directory is called ‘src’ (short for source), this is the directory where our source code will live. The second directory is called ‘test’, this is where our unit tests will live.
Use the ‘cd’ command to navigate into the src directory and run the command ‘dotnet new console’ to create our .NET project in the src directory.
Once again, you can use the dir command to list the contents of the src directory. You can see that several files and folders have been created.
This is your .NET project.
In this part of the series, we learned how to create a new .NET project using the dotnet new command and a template option.
In the next part of the series, we’ll explore the structure of the .NET project and learn how to run our project from the Command Prompt window. You can find that part of the series here.
Missed a part of the series? You can find an overview of the series and links to all previous parts on the index page.