DEV Community 👩‍💻👨‍💻

Cover image for Optimize Visual Studio Code for C# .NET Core development
⎝Dominic Böttger⎠
⎝Dominic Böttger⎠

Posted on • Originally published at dominic-boettger.com

Optimize Visual Studio Code for C# .NET Core development

Visual Studio Code is my favorite Code Editor and I like that it's so lightweight and also wanted to use it for C# development instead of the full-blown Visual Studio experience.

Some time ago I tried to use Visual Studio Code instead but I was missing some things to improve the development workflow. I wanted to have features like live unit testing and better guidance while coding C#. I tested some plugins and some configurations and now I think I found a solution to fulfill my needs.

Some of the extensions I use:

  1. C# - This is THE C# extension for Visual Studio Code powered by OmniSharp.
  2. .NET Core Test Explorer - An important extension to have live testing while coding in VScode.
  3. Auto-using - Automatically adds the dependencies with using in the head of the current file.
  4. C# FixFormat - It helps to automatically format the code. With the right settings, the code is also formatted when saving the file.
  5. Coverage Gutters - Show the code coverage visualized with color in the editor.
  6. .NET Core Extension Pack - Bundle of helpful extensions.
  7. .NET Core Snippet Pack - A pack of around 120 snippets for .NET Core.
  8. Fakesharper - Integrates Resharper into VSCode
  9. EditorConfig - Overwrites the VScode settings with the settings provided in the .editorconfig file.

The extensions were one part of the solution.

The other part was the settings. I provide the settings for VSCode with every project to install the recommended modules, activate the Roslyn Analyzer feedback ( which provides the StyleCop information,... ), activate the TestExplorer to use the Live Testing feature and activate the automatic format of the code.

The important settings in my .vscode/settings.json in the project are typically the following:

  "dotnet-test-explorer.testProjectPath": "**/*Test.csproj",
  "dotnet-test-explorer.autoWatch": true,
  "dotnet-test-explorer.testArguments": "/p:CollectCoverage=true /p:CoverletOutputFormat=lcov /p:CoverletOutput=./lcov.info",
  "cSpell.words": [
      "Xunit"
  ],
  "omnisharp.enableRoslynAnalyzers": true,
  "csharpfixformat.sort.usings.enabled": true,
  "csharpfixformat.sort.usings.systemFirst": false,
  "csharpfixformat.style.braces.onSameLine": false,
  "csharpfixformat.style.spaces.beforeParenthesis": false,
  "csharpfixformat.style.spaces.afterParenthesis": false,
  "editor.formatOnSave": true,
  "[csharp]": {
      "editor.defaultFormatter": "Leopotam.csharpfixformat"
  }

If you add StyleCop.Analyzers via NuGet to your projects the "omnisharp.enableRoslynAnalyzers": true - setting will provide this information in the editor.

To adjust the StyleCop ruleset and use the Ruleset in the root folder append the path to the ruleset in the csproj of all projects.

<PropertyGroup>
  <CodeAnalysisRuleSet>..\StyleCop.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>

To get the coverage statistics working it's also needed to add the coverlet.msbuild package to the Test project.

A complete example template is available under github.com/dominicboettger/vscode-csharp-template

Top comments (0)

Want to Create an Account?
Now it's your turn!
 
🗒 Share a tutorial
🤔 Reflect on your coding journey
❓ Ask a question

Create an account to join hundreds of thousands of DEV members on their journey.