Format Document in Visual Studio 2019 - C#

Shervan360 1,481 Reputation points
2021-10-25T13:39:14.003+00:00

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

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,198 questions
0 comments No comments
{count} votes

Accepted answer
  1. Michael Taylor 47,711 Reputation points
    2021-10-25T14:12:51.297+00:00

    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.

    1 person found this answer helpful.
    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Castorix31 81,461 Reputation points
    2021-10-25T13:49:58.067+00:00

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

    143449-formatdocument.jpg

    0 comments No comments

  2. Shervan360 1,481 Reputation points
    2021-10-25T13:56:05.583+00:00