为什么 Visual Studio 在用 "Comment Selection" 注释多行选择时求助于单行注释?
Why does Visual Studio resort to single-line comments when commenting a multi-line selection with "Comment Selection"?
关于 Visual Studio 中的 评论选择 选项,我一直想知道的一些小问题 (Ctrl + K, Ctrl + C)。
我在评论这个方法的实现时使用的是单行评论格式。
private void Foo()
{
//Bar b = new Bar();
}
当我在此处(部分行)注释来自构造函数的参数时,使用分隔注释格式。
private void Foo(Qux q)
{
Bar b = new Bar(/*q*/);
}
虽然注释掉整个方法,结果如下:
//private void Foo()
//{
// Bar b = new Bar();
//}
我觉得分隔注释格式在最后一种情况下更合适,因为规范说:
Single-line comments extend to the end of the source line. Delimited comments may span multiple lines.
有谁知道为什么在 Visual Studio 中评论 多行选择 时选择这种格式作为默认格式?
据我了解,多行注释是 C 语言的产物,大多数 C# 程序员更喜欢使用 //
注释风格。
如果在嵌套结构中多次使用,多行样式 /**/
会变得混乱,因此比单行样式更容易出错。 See this question 获取更多信息。
这样做会有一些问题:
如果 任何 代码行中有一个 */
,它将不起作用:
private void Foo(Qux q)
{
//we use "*/image/*" flag here to find only images
Bar b = new Bar("Some wildcard: */image/*");
}
评论给:
/*
private void Foo(Qux q)
{
//we use "*/image/*" flag here to find only images
Bar b = new Bar("Some wildcard: */image/*");
}
*/
如果您在已经包含定界注释的部分点击 "Comment Selection",则尝试用定界注释包装代码是行不通的:
/*
private void Foo(Qux q)
{
/* Some multiline
* comment
*/
Bar b = new Bar();
}
*/
但是很好,我们可以通过插入多个分隔注释和单行注释的组合来解决这个问题:
/*
private void Foo(Qux q)
{
/* Some multiline
* comment
*/
// */
/*
Bar b = new Bar();
}
*/
有点难看,但它确实有效。如果你看到那个注释代码,你是否能够立即识别代码部分是什么以及注释部分是什么?此外,如果你点击 "Uncomment Selection" 命令,你知道你会得到什么吗?
更进一步,想象一下,如果您评论此评论,它会变得更加丑陋和难以阅读。
workaround/escape 如果您在您的文本中有 */
评论,那么评论会变得更糟(在我看来):
private void Foo(Qux q)
{
//we use "*/image/*" flag here to find only images
Bar b = new Bar("Some wildcard: */image/*");
}
转换为:
/*
private void Foo(Qux q)
{
//we use "**//*/image/*" flag here to find only images
Bar b = new Bar("Some wildcard: **//*/image/*");
}
/*
将上面混淆的注释代码与现有的单行实现进行比较:
//private void Foo(Qux q)
//{
// /* Some multiline
// * comment
// */
// Bar b = new Bar();
//}
//private void Foo(Qux q)
//{
// //we use "*/image/*" flag here to find only images
// Bar b = new Bar("Some wildcard: */image/*");
//}
这样做的好处是代码几乎 1:1 与之前的样子完全一样,只是前缀为 //
个字符。仍然完全可读并且仍然完全可以预测如果您进一步评论或取消评论它会是什么样子。嵌套的单行注释或嵌套的分隔注释没有任何问题。
也许最后,从IDE的角度来看,它真的,真的很容易实现:"Comment Selection"意味着为每个添加一个//
前缀行,"Uncomment Selection" 表示删除前面的 //
。不用担心解析 code/comments 或解析 不正确的语法 code/comments.
多行注释已在 C# 中更改,但未在 VS 中实现 IDE,您可能可以创建自己的方式或找到扩展。 C# 现在使用 /** */ 进行多行注释,并且还有关于其工作原理的各种内置规则。
看到这个 link:https://msdn.microsoft.com/en-us/library/5fz4y783.aspx
关于 Visual Studio 中的 评论选择 选项,我一直想知道的一些小问题 (Ctrl + K, Ctrl + C)。
我在评论这个方法的实现时使用的是单行评论格式。
private void Foo()
{
//Bar b = new Bar();
}
当我在此处(部分行)注释来自构造函数的参数时,使用分隔注释格式。
private void Foo(Qux q)
{
Bar b = new Bar(/*q*/);
}
虽然注释掉整个方法,结果如下:
//private void Foo()
//{
// Bar b = new Bar();
//}
我觉得分隔注释格式在最后一种情况下更合适,因为规范说:
Single-line comments extend to the end of the source line. Delimited comments may span multiple lines.
有谁知道为什么在 Visual Studio 中评论 多行选择 时选择这种格式作为默认格式?
据我了解,多行注释是 C 语言的产物,大多数 C# 程序员更喜欢使用 //
注释风格。
如果在嵌套结构中多次使用,多行样式 /**/
会变得混乱,因此比单行样式更容易出错。 See this question 获取更多信息。
这样做会有一些问题:
如果 任何 代码行中有一个 */
,它将不起作用:
private void Foo(Qux q)
{
//we use "*/image/*" flag here to find only images
Bar b = new Bar("Some wildcard: */image/*");
}
评论给:
/*
private void Foo(Qux q)
{
//we use "*/image/*" flag here to find only images
Bar b = new Bar("Some wildcard: */image/*");
}
*/
如果您在已经包含定界注释的部分点击 "Comment Selection",则尝试用定界注释包装代码是行不通的:
/*
private void Foo(Qux q)
{
/* Some multiline
* comment
*/
Bar b = new Bar();
}
*/
但是很好,我们可以通过插入多个分隔注释和单行注释的组合来解决这个问题:
/*
private void Foo(Qux q)
{
/* Some multiline
* comment
*/
// */
/*
Bar b = new Bar();
}
*/
有点难看,但它确实有效。如果你看到那个注释代码,你是否能够立即识别代码部分是什么以及注释部分是什么?此外,如果你点击 "Uncomment Selection" 命令,你知道你会得到什么吗?
更进一步,想象一下,如果您评论此评论,它会变得更加丑陋和难以阅读。
workaround/escape 如果您在您的文本中有 */
评论,那么评论会变得更糟(在我看来):
private void Foo(Qux q)
{
//we use "*/image/*" flag here to find only images
Bar b = new Bar("Some wildcard: */image/*");
}
转换为:
/*
private void Foo(Qux q)
{
//we use "**//*/image/*" flag here to find only images
Bar b = new Bar("Some wildcard: **//*/image/*");
}
/*
将上面混淆的注释代码与现有的单行实现进行比较:
//private void Foo(Qux q)
//{
// /* Some multiline
// * comment
// */
// Bar b = new Bar();
//}
//private void Foo(Qux q)
//{
// //we use "*/image/*" flag here to find only images
// Bar b = new Bar("Some wildcard: */image/*");
//}
这样做的好处是代码几乎 1:1 与之前的样子完全一样,只是前缀为 //
个字符。仍然完全可读并且仍然完全可以预测如果您进一步评论或取消评论它会是什么样子。嵌套的单行注释或嵌套的分隔注释没有任何问题。
也许最后,从IDE的角度来看,它真的,真的很容易实现:"Comment Selection"意味着为每个添加一个//
前缀行,"Uncomment Selection" 表示删除前面的 //
。不用担心解析 code/comments 或解析 不正确的语法 code/comments.
多行注释已在 C# 中更改,但未在 VS 中实现 IDE,您可能可以创建自己的方式或找到扩展。 C# 现在使用 /** */ 进行多行注释,并且还有关于其工作原理的各种内置规则。 看到这个 link:https://msdn.microsoft.com/en-us/library/5fz4y783.aspx