.NET Solutions and Projects for C#
This publication is a follow up of the VSCode C# setup post
.NET Solutions
The dotnet tool allows us to create projects with the dotnet new subcommand,
and because of that it is easy to end up with many projects mixed everywhere.
It is a common practice in C# to group related projects inside a Solution folder to have them well organized.
This workflow can also be handled by the dotnet tool in this way:
-
Create a new Solution inside
<SolutionName>folder to contain a group of related .NET projectsdotnet new sln --output <SolutionName>Note: PascalCase (initial uppercase letters) is preferred for Solution names
-
Go inside the Solution folder
cd <SolutionName> -
Verify that there is a
.slnfile (eg. using thelscommand) -
Create a new project inside
<ProjectName>subfolder:dotnet new console --output <ProjectName>Also add the project to the
.slnconfig:dotnet sln add <ProjectName>Note: this should be done for each project we want to create
-
Modify the project code as needed
-
Build and execute the project using the following command
dotnet run --project <ProjectName> -
A project can also be removed from the Solution:
dotnet sln remove <ProjectName>The remaining files should be removed by hand:
rm -rf <ProjectName>Note: be careful with
rm, deleted files will disappear forever! 😱
VSCode and .NET Solutions
This editor also has support for .NET Solutions if the correct extension is installed
-
Go inside the Solution folder and start a VSCode instance
cd <SolutionName> code . -
If VSCode asks about adding assets, just ignore the message!
- Note: the next section will show how to generate the assets for each specific project
-
Click the Extensions tab on the left toolbar
-
Look for the vscode-solution-explorer extension and install it
-
A new Solution tab should appear on the left toolbar
-
Click the Solution tab, a list of projects will be shown
-
Right-click any project in the list to see the context menu
-
Select
SolutionExplorer: Runto build and execute that project -
The context menues also allow other options:
- Add a new project
- Create a file (ie. class, interface, etc)
- Remove a project from the Solution
- etc ...
Run & Debug a single Project
To use Run & Debug features the easiest option is to open a single Project
subfolder instead of a Solution folder.
-
Go inside a Project folder and start a VSCode instance
cd <SolutionName>/<ProjectName> code . -
If VSCode asks about adding assets, this time say yes and jump to step 6
- Note: this creates a
.vscodesubfolder with the required configurations
- Note: this creates a
-
Otherwise open a
.csfile to launch C# extension -
Go to Run & Debug tab on the left toolbar
-
Click on
Generate C# Assets for Build and Debug- Note: this option only appears if
.vscodeassets were not created yet
- Note: this option only appears if
-
Press
F5or theStart Debuggingbutton in the Run & Debug tab- Note: this step requires the assets to be already inside the
.vscodesubdirectory
- Note: this step requires the assets to be already inside the
Happy hacking! 🐱