.NET 9 continues the tradition of Microsoft's commitment to performance, reliability, and developer productivity. With every iteration, .NET has gained features and optimizations that make it one of the leading platforms for modern application development. But unfortunately the most powerful IDE of .NET development Visual Studio isn't available on macOS.
Microsoft officially announced that they will no longer support Visual Studio for macOS and made VS Code the de-facto standard IDE for cross platform C# development. According to Microsoft official statement -
"While we have retired Visual Studio for Mac, we remain committed to our developers on Mac and .NET MAUI with alternatives like the C# Dev Kit for Visual Studio Code and other extensions you can use to take advantage of our ongoing investments in .NET development."
While VS Code with C# Dev Kit is a great choice but Visual Studio developers may need some guidance to effectively use VS Code with the C# Dev Kit. This blog post aims to provide a comprehensive guide to making .NET development on macOS feel like a native experience.
Install C# Extensions in VS Code
Here are some VS Code extensions to enhance your C# and .NET development experience.
- C# Dev Kit
- C# - Base Language Support
- IntelliCode for C# Dev Kit
After installing the extensions the command palette of VS Code is supercharged with .NET related commands i.e. Create a New Project, Opening an Existing Solution, etc. So lets inspect the changes with (Command + Shift + P) and type .NET in the command palette.
Although the command palette offers a way to manage projects and solutions, using the .NET CLI directly can boost developer productivity.
The Concept of Solution and Project in .NET
In the .NET ecosystem, Solution and Project are foundational organizational concepts used within Visual Studio and other .NET development environments. They help developers manage the structure, dependencies, and workflow of software development.
Project:
A Project in .NET represents a single, self-contained unit of work or a collection of source files, references, and resources. It is essentially the building block of a .NET application. Defined by a .csproj or .vbproj file (depending on the language). For instance console project, web api project etc.
Solution:
A Solution is a container for organizing one or more related Projects. It provides a higher-level view of the application or system being developed. Defined by a .sln file and contains reference to multiple projects.
A Simple representation of Solution and Project Hierarchy
MySolution.sln
│
├── WebApp.csproj
│ ├── Controllers
│ ├── Views
│ └── Models
│
├── DataAccess.csproj
│ └── Repositories
│
└── Tests.csproj
└── UnitTests
Managing Development with .NET CLI
Let's create an empty solution first so that we can add one or more projects later on. Create a folder and open the folder with VS Code. Inside the VS Code terminal run -
> dotnet new sln -n VsCodePlay
I have named the solution VsCodePlay, and an empty solution has been created named "VsCodePlay.sln".
Let's create a webapi project inside this solution with this command -
> dotnet new webapi -n Todo.API
A webapi project with all the weatherforecast boilerplate has been created but it's still not part of VsCodePlay.sln, We can inspect that in Solution Explorer of VS Code.
So run this command to add a project into a solution, The command should be run inside the root folder of the solution. In out case VsCodePlay folder -
> dotnet sln VsCodePlay.sln add Todo.API/Todo.API.csproj
I've included a second console application in the solution. The Solution Explorer now reflects this addition, as shown below. You can manage the application from the Solution Explorer alike Visual Studio.
You've already noticed that dotnet new
command can create the boilerplate for you with just a single command. For inspecting the available project types in .NET you can run -
> dotnet new list
Using the .NET CLI, you can quickly generate a fully functional, basic project structure (boilerplate) for various project types, simply by using their short names.
We are now well-prepared to organize our project using the .NET CLI. Let’s explore some essential commands that will help streamline and manage our development process effectively.
Run the project in development mode
> dotnet run
If you don't want to restart the server manually for reflecting file changes then run the project in watch mode, It will continuously watch the file changes and automatically restart the server.
> dotnet watch run
Install a Nuget Package
> dotnet add package Newtonsoft.Json
Remove a Nuget Package
> dotnet remove package Newtonsoft.Json
Building a project for debugging
> dotnet build
Cleaning an existing build
> dotnet clean
Publishing a project as DLL in production
> dotnet publish "./Todo.API.csproj" -c Release -o ./publish
Publishing an entire solution
> dotnet publish -c Release
This command needs to be executed at the solution level.
Run the project in production mode
> dotnet ./publish/Todo.API.dll
Conclusion
VS Code, complemented by essential extensions and the .NET CLI, is a powerful tool for cross-platform C# application development. The insights shared in this post can help developers embrace .NET development on macOS, unlocking opportunities for innovation and project success.
Top comments (0)