c# - else 语句之前奇怪的自动缩进

标签 c# visual-studio

在使用 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 + ECTRL + D 后,所有内容都会自动正确缩进(在我的意见):

        if (condition)
        {

        }
        // comment line
        // another one
        else if (condition)
        {

        }

关于c# - else 语句之前奇怪的自动缩进,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12753886/

相关文章:

c# - 如何将长数组转换为字节数组?

c# - 如何让 Resharper 将成员上的 "private"和类上的 "internal"显示为冗余错误?

c# - 自适应用户界面/环境算法

visual-studio - 如何使用 ReSharper 更改 Visual Studio 中的突出显示颜色?

c# - 无法解析程序集或 Windows 元数据文件 "Mono.Android.dll"

c# - 错误 : AWS Identity and Access Management user accounts cannot be used for Amazon Mechanical Turk

c# - WaitOne() 永远等待,即使所有事件都已触发

c++ - MSVC 2017 中 STL 容器的移动构造函数未标记为 noexcept

visual-studio - Visual Studio 项目到 MonoDevelop

c# - Android Studio 与 Visual Studio(跨平台原生移动应用开发)