Visual Studio 在 if 语句后添加空格
有时(但并非总是!?)当我粘贴或用分号结束一行时,Visual Studio 会在 if
语句后添加一个空格!例如,粘贴时,这
if()
将变成这样:
if (condition)
并且,完成一行时,这
if(condition)
DoSomething()
将变成这样:
if (condition)
DoSomething();
必须不断删除此空格(有时两次!),这让我绝对疯狂!似乎发生在 if
和 while
中,但不是 for
。
我在 VS 选项中找不到与此相关的任何内容。我确实安装了 Resharper,但它设置为不自动添加空间:
为什么 Visual Studio 会惩罚我?
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:
Why is Visual Studio punishing me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在 Visual Studio 选项中的“文本编辑器/C#/格式/间距”下,“设置其他间距选项”部分中,有一个“在控制流语句中的关键字后插入空格”的选项。
我怀疑你会发现你已经检查了这一点,但你不希望这样。 (我特意检查过:)
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 :)
根据需要禁用自动格式化。
默认情况下,我的 C# Express 2010 配置中会检查所有内容:
Disable auto-formatting as you see fit.
All are checked by default in my C# Express 2010 config:
相关的 EditorConfig 代码样式规则选项< C# 的 /a> 如下:
此规则在 C# 格式选项页面。
这允许您为每个项目设置此选项,而不是更改 Visual Studio 安装中的全局选项。
The relevant EditorConfig code style rule option for C# is the following:
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.
Visual Studio 的行为似乎有点不一致。我在这里测试了所有内容,并在选项中选中了“自动格式化”设置,如下图所示:
我使用了以下代码:
如果我将上面的代码复制/粘贴到 Visual Studio 中, Visual Studio 将自动应用格式 - 导致
if
后面出现一个空格。如果我手动键入代码,Visual Studio 不会自动在
if
之后添加空格,即使在我使用右括号完成 if 行之后,甚至在我使用右括号完成 if 块之后也是如此。正如另一个答案中提到的,如果我按 CTRL+K+D 格式化整个文档,Visual Studio 会在 if 后面插入一个空格。
这有点令人沮丧,因为如果我手动编写代码,则 if 后面没有空格(出于习惯),但如果我粘贴代码或格式化文档,我就会得到空格。
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:
I used the following code:
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.
另一个好技巧是按原样保留编码并按 CTRL+K+D 它将自动格式化您的代码并修复空格
Another good trick is leaving the coding as is and hit CTRL+K+D it will auto-format your code and fix the spaces