OP 问题:“除了个人喜好之外,是否有任何理由使用 // 而不是 # 用于 cmets?”
一个 2021 年的答案,这肯定不是我们在此线程中看到的唯一答案:
如果您使用 Visual Studio Code 并使用区域来阻止代码,则必须使用 # 而不是 // 来定义区域。对于这个问题,不,即使对于这个用例:如果您要注释掉一个区域,您可以使用# 或// 或/** */,您用于此的技术是个人的偏好。
VSCode 中的块定义示例:
#region this is a major block
/** DocBlock */
function one() {}
/** DocBlock */
function two() {
#region nested region based on indentation
// comments and code in here
# another nested region based on indentation
// foo
#endregion
#endregion
}
#endregion
关于内块的折叠:
#region this is a major block
/** DocBlock */
function one() {}
/** DocBlock */
function two() {
> #region nested region based on indentation
}
#endregion
关于外块的折叠:
> #region this is a major block
我引用了以下具体用法,人们可能会尝试尝试,但这些都行不通。事实上,这正是您禁用#region 块的方式:
// #region
// #endregion
/** #region */
/** #endregion */
关于在 VSCode 中注释掉一个区域:
/** You can now collapse this block
#region Test1
// foo
#endregion
// everything through to here is collapsed
*/
// #region Test1
// folding is disabled here
// #endregion
# #region Test1
// this also disables the fold
# #endregion
所有这些都说,“除了个人喜好之外,还有什么理由使用 // 而不是 # 来表示 cmets?” 我同意这个线程和 other thread 中的 cmets :// 更普遍地被识别和使用,这通常是使用这种评论风格而不是 # 的好理由。
最后注意,基于缩进的嵌套要小心,因为代码格式化会删除您的手动缩进,从而破坏您基于 cmets 的嵌套块方案。我已经用# 和// 对此进行了测试(顺便说一句,// 也嵌套在缩进上。同样,在 OP 问题的上下文中,不,没有理由使用// 而不是#用于当前 VSCode 中此上下文中的嵌套缩进,因为两者的工作方式完全相同。但是,这是使用 # 而不是 // 的用例。
Ref - 无需扩展,已在 1.62.3 中验证。也可以查看关于缩进的注释。