代码编辑

 

代码编辑

https://docs.microsoft.com/zh-cn/visualstudio/get-started/csharp/tutorial-editor?view=vs-2017

 

 

为代码添加注释Comment out code

工具栏是 Visual Studio 菜单栏下的一行按钮,有助于提高编码效率。The toolbar, which is the row of buttons under the menu bar in Visual Studio, can help make you more productive as you code. 例如,可以切换 IntelliSense 完成模式(IntelliSense 是一种编码辅助工具,可显示匹配方法列表以及其他内容),增加或减少行缩进,或标注出不想编译的代码。For example, you can toggle IntelliSense completion mode (IntelliSense is a coding aid that displays a list of matching methods, amongst other things), increase or decrease a line indent, or comment out code that you don't want to compile. 在本部分中,我们将标注出部分代码。In this section, we'll comment out some code.

编辑器工具栏

  1. 将以下代码粘贴到 Main() 方法主体中。Paste the following code into the Main() method body.

    C#复制

    // _words is a string array that we'll sort alphabetically
    string[] _words = {
        "the",
        "quick",
        "brown",
        "fox",
        "jumps"
    };
    
    string[] morewords = {
        "over",
        "the",
        "lazy",
        "dog"
    };
    
    IEnumerable<string> query = from word in _words
                                orderby word.Length
                                select word;
    
  2. 我们现在没有使用 morewords 变量,但稍后可能会用到,所以我们不想彻底删除它。We're not using the morewords variable, but we may use it later so we don't want to completely delete it. 那我们就来为这些行加上注释。Instead, let's comment out those lines. 选择整个 morewords 定义直到结束分号,然后选择工具栏上的“为选定行添加注释”。Select the entire definition of morewords to the closing semi-colon, and then choose the Comment out the selected lines button on the toolbar. 如果想要使用键盘,请按 Ctrl+K、Ctrl+C。If you prefer to use the keyboard, press Ctrl+K, Ctrl+C.

    “添加注释”按钮

    C# 注释字符 // 添加到了每个所选行的开始处,从而为代码添加注释。The C# comment characters // are added to the beginning of each selected line to comment out the code.

折叠代码块Collapse code blocks

我们不想看到生成的 Class1 的空构造函数,所以为了让代码更整洁,我们将其折叠。We don't want to see the empty constructor for Class1 that was generated, so to unclutter our view of the code, let's collapse it. 在构造函数第一行的边距中选择内部带有减号的小灰色框。Choose the small gray box with the minus sign inside it in the margin of the first line of the constructor. 如果使用的是键盘,也可将光标置于构造函数代码中的任意位置,然后按 Ctrl+M、Ctrl+M。Or, if you're a keyboard user, place the cursor anywhere in the constructor code and press Ctrl+M, Ctrl+M.

“大纲显示折叠”按钮

代码块折叠到第一行,后跟省略号 (...)。The code block collapses to just the first line, followed by an ellipsis (...). 若要再次展开代码块,请单击现在带有加号的相同灰色框,或者再次按 Ctrl+M,Ctrl+M。To expand the code block again, click the same gray box that now has a plus sign in it, or press Ctrl+M, Ctrl+M again. 此功能被称为大纲显示,在折叠长方法或整个类时特别有用。This feature is called Outlining and is especially useful when you're collapsing long methods or entire classes.

查看符号定义View symbol definitions

通过 Visual Studio 编辑器可轻松查看类型、方法等的定义。一种方法是导航到包含定义的文件,例如通过选择“转到定义”,转到引用符号的任何位置。The Visual Studio editor makes it easy to inspect the definition of a type, method, etc. One way is to navigate to the file that contains the definition, for example by choosing Go to Definition anywhere the symbol is referenced. 使用“速览定义”速度更快,不会干扰你处理文件。An even quicker way that doesn't move your focus away from the file you're working in is to use Peek Definition. 我们来快速查看一下 string 类型的定义。Let's peek at the definition of the string type.

  1. 右键单击出现的任意 string,然后选择内容菜单上的“速览定义”。Right-click on any occurrence of string and choose Peek Definition from the content menu. 或者,按 Alt+F12。Or, press Alt+F12.

    此时会出现一个弹出窗口,其中包含 String 类的定义。A pop-up window appears with the definition of the String class. 可在弹出窗口中滚动,甚至还可从速览的代码中查看另一类型的定义。You can scroll within the pop-up window, or even peek at the definition of another type from the peeked code.

    “速览定义”窗口

  2. 选择弹出窗口右上方的“x”小框,关闭“速览定义”窗口。Close the peeked definition window by choosing the small box with an "x" at the top right of the pop-up window.

使用 IntelliSense 完成单词Use IntelliSense to complete words

编写代码时,IntelliSense 是非常宝贵的资源。IntelliSense is an invaluable resource when you're coding. 它可显示某个类型的可用成员信息,或某个方法不同重载的参数详情。It can show you information about available members of a type, or parameter details for different overloads of a method. 还可用于完成单词,从而在输入大量字符后消除字符带来的歧义。You can also use IntelliSense to complete a word after you type enough characters to disambiguate it. 添加代码行,将有序字符串呈现到控制台窗口,这是程序输出的标准位置。Let's add a line of code to print out the ordered strings to the console window, which is the standard place for output from the program to go.

  1. query 变量下,开始键入以下代码:Below the query variable, start typing the following code:

    C#复制

    foreach (string str in qu
    

    IntelliSense 会显示有关 query 符号的“快速信息”。You see IntelliSense show you Quick Info about the query symbol.

    Visual Studio 中的 IntelliSense 文字自动完成

  2. 若要使用 IntelliSense 文字自动完成功能插入单词 query 的剩余部分,请按 Tab。To insert the rest of the word query by using IntelliSense's word completion functionality, press Tab.

  3. 完成后,代码块如以下代码所示。Finish off the code block to look like the following code. 你甚至可以通过输入 cw,然后按 Tab 两次来生成 Console.WriteLine 代码,再次练习使用代码片段。You can even practice using code snippets again by entering cw and then pressing Tab twice to generate the Console.WriteLine code.

    C#复制

    foreach (string str in query)
    {
       Console.WriteLine(str);
    }
    

重构名称Refactor a name

没有谁能一次就得到正确的代码,代码中可能必须要更改的一项内容是变量或方法的名称。Nobody gets code right the first time, and one of the things you might have to change is the name of a variable or method. 我们来试试 Visual Studio 的重构功能,将 _words 变量重命名为 words。Let's try out Visual Studio's refactor functionality to rename the _words variable to words.

  1. 将光标置于 _words 变量的定义上,然后从右键菜单或上下文菜单中选择“重命名”,或按 Ctrl+R,Ctrl+R。Place your cursor over the definition of the _words variable, and choose Rename from the right-click or context menu, or press Ctrl+R, Ctrl+R.

    此时编辑器右上角会弹出一个“重命名”对话框。A pop-up Rename dialog box appears at the top right of the editor.

  2. 输入所需名称“words”。Enter the desired name words. 请注意,查询中对 words 的引用也会自动重命名。Notice that the reference to words in the query is also automatically renamed. 在按 Enter 前,请在“重命名”弹出框中选中“包含注释”复选框。Before you press Enter, select the Include comments checkbox in the Rename pop-up box.

    “重命名”对话框

  3. Enter。Press Enter.

    出现的两处 words 均被重命名,代码注释中对 words 的引用也被重命名。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值