Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio Code doesn't format C# code

I created a new file, set the C# language, and wrote some code. Then I pressed Ctrl + Shift + F (or F1Format Document). And got I the error

Sorry, but there is no formatter for 'csharp'-files installed.

Also, I installed C# Extension, but it didn't help. The Visual Studio Code version is 1.18.0.

like image 288
andoral Avatar asked Nov 17 '17 14:11

andoral


People also ask

How do I fix code formatting in Visual Studio?

The code formatting is available in Visual Studio Code through the following shortcuts: On Windows Shift + Alt + F. On Mac Shift + Option + F. On Linux Ctrl + Shift + I.

How do I fix prettier in VSCode?

Open up VSCode settings in UI mode by selecting File > Preferences > Settings or press Ctrl + ,. Input “formatter” in the search box. Once you see Editor: Default Formatter section, select . This setting allows you to define a default formatter which takes precedence over all other formatter settings.


1 Answers

If you have prettier as the default formatter as I do, you should do this:

  1. Open your vscode settings with these shortcut: ctrl + ,, or enter image description here

  2. Then click to "open settings (JSON)": enter image description here

  3. This is where you should paste the below snippet.

This is my config:

{   "editor.defaultFormatter": "esbenp.prettier-vscode",   "[csharp]": {     "editor.defaultFormatter": "ms-dotnettools.csharp"   } } 

For you to apply this configuration you need c# extension.

like image 115
ezhupa99 Avatar answered Sep 23 '22 23:09

ezhupa99