Visual Studio adding space after if-statements
Asked Answered
T

5

25

Sometimes (but not always!?) when I paste or end a line with a semi-colon, Visual Studio will add a space after the if statement! For example, when pasting, this

if()

will turn into this:

if (condition)

and, when completing a line, this

if(condition)
    DoSomething()

will turn into this:

if (condition)
    DoSomething();

Having to constantly delete this space (sometimes twice!) is driving me absolutely Bonkers! It seems to happen with if and while but not for.

I cannot find anything relating to this in the VS options. I do have Resharper installed, but it is set to not add the space automatically: (image of my resharper settings)

Why is Visual Studio punishing me?

Teplitz answered 17/11, 2010 at 19:19 Comment(2)
Your formatting preferences look pretty strange to me, but +1 anyway for "Why is Visual Studio punishing me?"Dogvane
I've heard that the "if ()" is part of the accepted C# standard, which is quite odd. Can someone verify this?Syllabism
N
37

In the Visual Studio options, under Text Editor / C# / Formatting / Spacing, in the "Set other spacing options" part, there's an option for "Insert space after keywords in control flow statements".

I suspect you'll find you've got that checked, and you don't want it to be. (I have it checked deliberately :)

Napalm answered 17/11, 2010 at 19:23 Comment(1)
Visual Studio 2022 (v17.7.5) is ignoring my "DON'T autoformat" settings and reformatting code when using Ctrl+K,/ to uncomment code.Physiological
A
5

Disable auto-formatting as you see fit.

Tools->Options->Text Editor->C#->Formatting->General.

All are checked by default in my C# Express 2010 config:

  • Automatically format completed statement on ;
  • Automatically format completed block on }
  • Automatically format on paste
Apiarist answered 17/11, 2010 at 19:23 Comment(0)
M
2

The relevant EditorConfig code style rule option for C# is the following:

csharp_space_after_keywords_in_control_flow_statements = false

This rule is fully explained on the C# Formatting Options page.

This allows you to set this option per-project, instead of changing a global option in your Visual Studio installation.

Mannerism answered 27/8, 2021 at 9:0 Comment(0)
S
1

Visual Studio behavior seems to be a little inconsistent here. I tested everything here with the "Automatically Format" settings all checked in the Options, as shown in the following image:

enter image description here

I used the following code:

if(true)
{
   int m = 3;
}

If I copy/paste the code above into Visual Studio, Visual Studio will automatically apply formatting - resulting in a space after if.

If I type the code in manually, Visual Studio will not automatically add a space after if, even after I complete the if line with a closing parenthesis, and even after I complete the if block with a closing bracket.

As mentioned in another answer, if I CTRL+K+D to format the entire document, Visual Studio does insert a space after if.

This is slightly frustrating because if I'm manually writing code I get no space after if (out of habit) but if I paste code or format my document I do get spaces.

Severus answered 4/9, 2017 at 16:58 Comment(1)
You can always ctl-z after pasting to remove the applied formatting.Clam
F
0

Another good trick is leaving the coding as is and hit CTRL+K+D it will auto-format your code and fix the spaces

Francescafrancesco answered 17/11, 2010 at 19:27 Comment(1)
Or it will mess up the spaces, if you don't have the settings correct.Aspirin

© 2022 - 2024 — McMap. All rights reserved.