Why won't my C# code format correctly in Visual Studio 2022? - Stack Overflow

admin2025-04-16  3

Even when I use the shortcut Ctrl+K Ctrl+D (Or Edit>Advanced>Format Document) in VS 2022, this C# code won't format properly:

Why won't the Visual Studio 2022 formatter fix these issues? How can I get this code to format correctly? Seems like a weird behavior.

Even when I use the shortcut Ctrl+K Ctrl+D (Or Edit>Advanced>Format Document) in VS 2022, this C# code won't format properly:

Why won't the Visual Studio 2022 formatter fix these issues? How can I get this code to format correctly? Seems like a weird behavior.

Share Improve this question asked Feb 3 at 13:31 MehdiwayMehdiway 10.7k8 gold badges37 silver badges68 bronze badges 3
  • 3 side note: List<string> myList = [ "Hello", "World", "From", "Morocco" ]; would actually be more efficient (it uses different APIs and bypasses some book-keeping that isn't going to be necessary in this scenario) – Marc Gravell Commented Feb 3 at 13:34
  • 1 There are many such quirks, where formatter tries to keep your custom formatting intact (going to a 0 position in next row is not default formatting), but will automatically add/remove indents. You will learn quickly to go to end of certain rows and press Enter there. Quickly fixing miss-formatting unfortunately not possible (at least with default formatter, there could be extensions which do), but once you fix it - it will stay. – Sinatr Commented Feb 3 at 14:01
  • This issue has been discussed since 2016, but there is still no good solution. – shingo Commented Feb 3 at 14:23
Add a comment  | 

1 Answer 1

Reset to default 0

Based on my test, I reproduced the same problem with you. As mentioned in the comments, this issue has been discussed since 2016, but there is still no good solution.

I recommend that you could report the problem in Developer Community. There are many VS developers who can help here. It is important to note that official staff may only handle this problem when enough people report it.

转载请注明原文地址:http://www.anycun.com/QandA/1744769131a87361.html