在使用 C# 的 Visual Studio 中(我在 2010 年和 2012 年都遇到过这种情况),如果我在 else
语句之前输入新行,就会出现奇怪的自动缩进:
if (condition1)
{
DoSomething();
} // press Enter here, after the brace
// auto indents to here
// Helpful comment here about condition2, but awkwardly auto-indented
else if (condition2)
{
DoSomethingElse();
}
直观上,我希望它仅自动缩进到与 if
/else
相同的列,但它反而进一步缩进了一级。
我意识到无论如何在这两个 block 之间放置其他代码是非法的,但我仍然想在那里添加注释。有谁知道为什么会发生这种情况或如何解决它?每当我尝试注释 else
block 时,都必须按退格键 4 次,这真是太奇怪和烦人了。
我知道有人会说“只需将注释放在 else
/if
block 中”,但我对实际的解释/解决方案感兴趣这个现象;此外,对我来说,解释两行前发生的代码的注释是没有意义的。
我也知道有人会说“只需将注释放在与 else
/if
block 同一行”,但将注释和代码放在同一行我觉得很恶心。
最佳答案
我刚刚在 Visual Studio 2012 中尝试过:
确实,当您编写评论时,它似乎没有正确缩进。
但是按 CTRL + E、CTRL + D 后,所有内容都会自动正确缩进(在我的意见):
if (condition)
{
}
// comment line
// another one
else if (condition)
{
}
关于c# - else 语句之前奇怪的自动缩进,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12753886/