为什么在用&“评论选择&"注释多行选择时,Visual Studio为什么使用单行注释? [英] Why does Visual Studio resort to single-line comments when commenting a multi-line selection with "Comment Selection"?

查看:48
本文介绍了为什么在用&“评论选择&"注释多行选择时,Visual Studio为什么使用单行注释?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于Visual Studio中的注释选择选项,我一直想知道的一些东西( Ctrl + K Ctrl + C ).

Something small that I've always wondered about regarding the Comment Selection option in Visual Studio (Ctrl + K, Ctrl + C).

当我注释此方法的实现时,使用单行注释格式.

When I comment this method's implementation single-line comment format is used.

private void Foo()
{
    //Bar b = new Bar();
}

当我注释来自构造函数的参数(部分行)时,使用分隔注释格式.

When I comment the parameters from the constructor here (partial line) delimited comment format is used.

private void Foo(Qux q)
{
    Bar b = new Bar(/*q*/);
}

注释掉整个方法的结果是:

While commenting out the entire method results in this:

//private void Foo()
//{
//    Bar b = new Bar();
//}

我觉得在最后一种情况下定界注释格式会更合适,因为该规范说:

I feel like the delimited comment format would be more appropriate in the last situation, since the spec says:

单行注释延伸至源代码行的末尾.定界注释可能跨越多行.

Single-line comments extend to the end of the source line. Delimited comments may span multiple lines.

有人知道在Visual Studio中注释多行选择时为什么选择默认格式吗?

Does anyone know why this was chosen as the default format when commenting a multi-line selection in Visual Studio?

推荐答案

这样做会有一些问题:

如果任何条代码行中都有 */,则该行将不起作用:

If any of the code lines had a */ located in it, it wouldn't work:

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/*");
}
*/


如果您在已经包含定界注释的部分上单击评论选择",则尝试用定界注释包装代码将不起作用:


If you were hitting "Comment Selection" on a section that already contained a delimited comment, then trying to wrap the code with a delimited comment wouldn't work:

/*
private void Foo(Qux q)
{
    /* Some multiline 
     * comment
     */
    Bar b = new Bar();
}
*/


但是很好,我们可以通过插入多个带分隔符的注释和单行注释的组合来解决此问题:


But fine, we can work around that by a combination of inserting multiple delimited comments and single-line comments:

/*
private void Foo(Qux q)
{
    /* Some multiline 
     * comment
*/
//   */
/*
    Bar b = new Bar();
}
*/

Kinda很难看,但是可以用.如果遇到该注释的代码,您是否能够立即识别出什么是代码部分以及注释部分是什么?此外,如果您按下取消注释选择"命令,您是否知道要获得什么?

Kinda ugly, but it works. If you came across that commented code, would you be able to immediately recognize what the code parts are and what the comment parts are? Further, if you hit the "Uncomment Selection" command, would you know what you were going to get?

再进一步,想象一下,如果您评论此评论,它将变得更加丑陋且更加难以理解.

Going further, imagine if you comment this comment, it would get even uglier and more unreadable.

如果您要注释的文本中有 */,则变通/转义注释会变得更糟(我认为):

It gets even worse (in my opinion) to workaround/escape the comments if you had */ in your text that you were commenting out:

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/*");
}
/*


将上面令人困惑的注释代码与现有的单行实现进行比较:


Compare the above confusing commented code to the existing single-line implementation:

//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,只是以//字符为前缀.如果您进一步评论或取消评论,它仍然是完全可读的并且仍然是完全可预测的.嵌套单行注释或嵌套定界注释都没有问题.

Benefit of this is that the code is pretty much 1:1 exactly what it looked like before, just prefixed with // characters. Still fully readable and still completely predictable what it would look like if you further commented or uncommented it. No issues whatsoever with nested single-line comments or nested delimited comments.

也许最后,从IDE的角度来看,它真的非常简单地实现:注释选择"意味着在每行取消注释选择"中添加一个//前缀.表示删除前面的//.不用担心解析代码/注释或解析不正确的语法代码/注释.

Perhaps finally, it's really, really simple to implement from an IDE standpoint: "Comment Selection" means add a // prefix to every line, "Uncomment Selection" means remove a preceding //. No mucking about with parsing code/comments, or parsing incorrect syntax code/comments.

这篇关于为什么在用&“评论选择&"注释多行选择时,Visual Studio为什么使用单行注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆