question

Shervan360-6172 avatar image
0 Votes"
Shervan360-6172 asked cooldadtx answered

Format Document in Visual Studio 2019 - C#

Hello,

I press Ctrl,K + Ctrl,D but it doesn't work and C# code doesn't format.

143436-screenshot-2021-10-25-093704.jpg


dotnet-csharp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

cooldadtx avatar image
1 Vote"
cooldadtx answered

Format document will format the document based upon the rules that are defined by the VS settings and any applied .editorconfig file. It will not however resolve all formatting issues as sometimes there is no valid formatting that makes sense.

In your specific case format document won't do anything because it doesn't see anything wrong. You have lined up your curly braces and indentation inside an initializer expression is not enforced. Hence the format command isn't going to do anything for it. In general, for a single block of code you can remove and add back a closing curly and/or a semicolon (and save if you have enabled the option) and the editor will reformat that block of code. If it doesn't do anything then format document probably won't either.

The only formatting I see that you might have wanted is to line up the object initializer expression with the declaration but that isn't an option in the VS formatting options AFAIK. You might be able to get this behavior using an extension such as CodeRush or ReSharper.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Castorix31 avatar image
0 Votes"
Castorix31 answered

Did you check in Options that the shortcut did not change :

143449-formatdocument.jpg



formatdocument.jpg (129.2 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Shervan360-6172 avatar image
0 Votes"
Shervan360-6172 answered Castorix31 commented

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I cannot reproduce this problem
(on Windows 10 1909, VS 2019 Version 16.11.2)

0 Votes 0 ·