C# 如何用空格或缩进注释多行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14027598/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 10:27:49  来源:igfitidea点击:

How to comment multiple lines with space or indent

c#visual-studio-2010comments

提问by KMC

In Visual Studio 2010, I have multiple lines of text to be commented:

在 Visual Studio 2010 中,我要注释多行文本:

A
B
C

Using CTRL+E+Cto comment out multiple lines, I get

使用CTRL+ E+C注释掉多行,我明白

//A
//B
//C

I would like to have a space (or indent) between //and A, hence // Ainstead of //A.

我想在//A之间有一个空格(或缩进),因此// A而不是//A.

However, after I group tab a block of text and indent it, CTRL+E+Cno longer comments out the selected text.

然而,当我组标签文字和缩进块吧,CTRL+ E+C不再评论了选定的文本。

How to group comment and get the following:

如何分组评论并获得以下内容:

// A
// B
// C

采纳答案by Habib

One way to do it would be:

一种方法是:

  1. Select the text, Press CTRL+ K, Cto comment (CTRL+E+C)
  2. Move the cursor to the first line after the delimiter //and before the Code text.
  3. Press Alt+ Shiftand use arrow keys to make selection. (Remember to make line selection(using down, up arrow keys), not the text selection - See Box Selection and Multi line editing)
  4. Once the selection is done, press space bar to enter a single space.
  1. 选择文本,按CTRL+ KC评论 ( CTRL+ E+ C)
  2. 将光标移动到分隔符之后//和代码文本之前的第一行。
  3. Alt+Shift并使用箭头键进行选择。(记住进行行选择(使用向下,向上箭头键),而不是文本选择 - 请参阅框选择和多行编辑
  4. 选择完成后,按空格键输入一个空格。

Notice the vertical blue line in the below image( that will appear once the selection is made, then you can insert any number of characters in between them)

注意下图中的垂直蓝线(一旦选择就会出现,然后您可以在它们之间插入任意数量的字符)

enter image description here

在此处输入图片说明

I couldn't find a direct way to do that. The interesting thing is that it is mentioned in the C# Coding Conventions (C# Programming Guide)under Commenting Conventions.

我找不到直接的方法来做到这一点。有趣的是,在Commenting Conventions 下的C# Coding Conventions(C# 编程指南)中提到了它。

Insert one space between the comment delimiter (//) and the comment text

在注释分隔符 (//) 和注释文本之间插入一个空格

But the default implementation of commenting in visual studio doesn't insert any space

但是visual studio中默认的注释实现不插入任何空格

回答by Jhankar Mahbub

Pressing Ctrl+K+Cor Ctrl+E+CAfter selecting the lines you want to comment will not give space after slashes. you can use multiline select to provide space as suggested by Habib

Ctrl+ K+CCtrl+ E+C选择要注释的行后,斜杠后不会留空格。您可以使用多行选择来提供 Habib 建议的空间

Perhaps, you can use /* before the lines you want to comment and after */ in that case you might not need to provide spaces.

也许,您可以在要注释的行之前和 */ 之后使用 /* ,在这种情况下您可能不需要提供空格。

/*
  First Line to Comment
  Second Line to Comment
  Third Line to Comment      
*/

回答by DrewMurr

I was able to achieve the desired result by using Alt + Shift + up/downand then typing the desired comment characters and additional character.

通过使用Alt + Shift + up/down然后输入所需的注释字符和附加字符,我能够达到预期的结果。

回答by Jonathan Pilling

Might just be for Visual Studio '15, if you right-click on source code, there's an option for insert comment

可能仅适用于 Visual Studio '15,如果您右键单击源代码,则可以选择 insert comment

This puts summarytags around your comment section, but it does give the indentation that you want.

这会summary在您的评论部分周围放置标签,但它确实提供了您想要的缩进。

回答by Nisargi Joshi

  • You can customize every short cut operation according to your habbit.
  • 您可以根据自己的习惯自定义每个快捷操作。

Just go to Tools > Options > Environment > Keyboard > Find the action you want to set key board short-cut and change according to keyboard habbit.

只需转到工具>选项>环境>键盘>找到要设置键盘快捷方式的操作并根据键盘习惯进行更改。