为什么Visual Studio中诉诸单行注释多行选择与&QUOT时评论,评论的选择和QUOT;? [英] Why does Visual Studio resort to single-line comments when commenting a multi-line selection with "Comment Selection"?

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

问题描述

小东西,我一直想知道关于注释选择的Visual Studio中的选项(控制 + <大骨节病> K ,<大骨节病>控制 + <大骨节病> 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?

推荐答案

会有这样几个问题:

如果的code行的任意的有 * / 位于它,它是行不通的:

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


如果你击中​​一节,已经包含一个分隔符的注释注释选择,然后试图总结了code用分隔符的注释是行不通的:


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

有点儿难看,但它的工作原理。如果你碰到了评论code,你能够立即识别什么code部分是什么注释部分是什么?此外,如果你点击取消注释选择命令,你会知道你将要得到什么?

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


比较上述混淆评论code到现有的单行实现:


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

这好处是,code是pretty的多1:1正是它看起来像以前一样,只是prefixed与 // 字符。不过完全可仍然是完全predictable它会是什么样子,如果你还评论或取消注释它。没有任何问题与嵌套单行注释或嵌套分隔符的注释。

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的角度看落实:注释选择是指添加 // preFIX每一行,取消注释选择是指删除preceding // 。没有摆弄解析code /意见,或解析的的语法不正确的code /注释。

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中诉诸单行注释多行选择与&QUOT时评论,评论的选择和QUOT;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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