简体   繁体   中英

Visual Studio Community C# code formatting

I am trying to format C# code of a WinForms .NET Core 7.0 project in Visual Studio Community:

格式不正确的代码的屏幕截图

private void Form1_Load(object sender, EventArgs e)
{
    Dictionary<String, String> Dictionary = new Dictionary<String, String>
    {
        { "operation", "login" },
        { "phone", "123"},
        { "country","456"}      ,
        {          "otp", "789"},
        {"language","111" }
    };
}

I have tried Ctrl+K, Ctrl+D , removing the last brace in the code and putting it back.

The extra spaced are not getting removed. Can these be removed in whole code automatically using a command? If not, is there a plugin / extension that can help in code formatting?

ReSharper works with VS Community and will reformat that case by either

  • removing the ; and re-entering it (format on typing)
  • or reformatting the whole file via Cleanup Code... on the context menu for the C# file.

You'd have to pay for ReSharper, though, unless you're a student, working on an open-source project, or otherwise qualify for the free license .

Visual Studio Community (and probably Pro and Enterprise) doesn't seem to reformat dictionaries regardless of what you seem to do with a .editorconfig file or what you have set in Tools | Options | Text Editor | C# | Code Style | Formatting | General .

There may be other extensions that do this. Perhaps you could find one on the Visual Studio Marketplace .

  1. Use the shortcut key Ctrl+f.
  2. Enter [^\S\r\n]{2,} in FIND
  3. The value in the "replace" is empty
  4. Select use regular expression(Alt+E)
  5. Click the Replace All button(Alt+A)
  6. Use the Auto Align shortcut (CTRL+K+D)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM