რედაქტირება

გააზიარეთ მეშვეობით


Learn to use the code editor

In this introduction to the Visual Studio code editor, you’ll add code to a file and explore how Visual Studio helps make writing, navigating, and understanding code easier.

If you haven't already installed Visual Studio, go to the Visual Studio downloads page to install it for free.

This article assumes you're already familiar with a programming language. If you aren't, we suggest you look at one of the programming quickstarts first, such as create a web app with Python or C#, or create a console app with Visual Basic or C++.

Prerequisites

Create a new code file

Create a new file and add some code to it.

  1. Open Visual Studio. Select the Esc key, or select Continue without code on the start window, to open the development environment.

  2. From the File menu on the menu bar, select New > File, or select the Ctrl+N keys.

  3. In the New File dialog box, under the General category, select C# Class, and then select Open.

    A new file opens in the editor with a basic C# class structure.

    Screenshot of a C# code file in Visual Studio 2022.

Use GitHub Copilot

GitHub Copilot acts as an AI pair programmer to provide autocomplete-style code completions and context-aware multi-line code suggestions, as you code, in real-time, right in the editor. GitHub Copilot turns natural language prompts including comments and method names into coding suggestions. You can view and incorporate suggestions from GitHub Copilot directly within the editor.

Let's use Copilot to generate code suggestions:

  1. Place your cursor just below the final closing brace } in the file.

  2. Type a natural language comment: // Add a method to add two numbers and Enter.

  3. GitHub Copilot generates a code suggestion for you. The suggested implementation shows in gray text.

  4. To accept the suggestion, select Tab.

    Animated screenshot that shows the code suggestions generated by GitHub Copilot for the user comment.

Let's use Copilot Chat to submit a coding-related question as a prompt:

  1. Select the GitHub Copilot badge in the upper-right corner of the IDE.

  2. Select Open Chat Window from the dropdown.

  3. Enter the following prompt in the chat window:

    
    Generate sample code for a simple C# method to add two numbers.
    
    
  4. Copilot Chat generates sample code in response to your prompt.

GitHub Copilot is powered by AI, so surprises and mistakes are possible. For more information, see GitHub Copilot FAQs.

Get started with GitHub Copilot in Visual Studio.

Use code snippets

Visual Studio provides useful code snippets that you can use to quickly generate commonly used code blocks. Code snippets are available for several programming languages, including C#, Visual Basic, and C++.

Now add the C# void Main snippet to your file.

  1. Place your cursor just above the final closing brace } in the file, and type the characters svm.

    A pop-up dialog box appears with information about the svm code snippet.

    Screenshot of an IntelliSense pop-up for a code snippet in Visual Studio 2022.

  2. Select the Tab key twice to insert the code snippet.

    You'll see the static void Main() method signature get added to the file. The Main() method is the entry point for C# applications.

Available code snippets vary by programming language. To view the available code snippets for your language, go to Edit > IntelliSense > Insert Snippet or select Ctrl+K, Ctrl+X keys, then select the folder for your programming language. For C#, the code snippet list looks like this:

Screenshot of an IntelliSense pop-up for a C# code snippet list.

The list includes snippets for creating a class, a constructor, a for loop, an if or switch statement, and more.

Comment out code

The Text Editor toolbar, the row of buttons under the menu bar in Visual Studio, helps make you more productive as you code. For example, you can toggle IntelliSense completion mode, increase or decrease a line indent, or comment out code you don't want to compile.

Screenshot of the Text Editor toolbar in Visual Studio 2022.

Let's comment out some code.

  1. Paste the following code into the Main() method body.

    // someWords is a string array.
    string[] someWords = {
        "the",
        "quick",
        "brown",
        "fox",
        "jumps"
    };
    
    string[] moreWords = {
        "over",
        "the",
        "lazy",
        "dog"
    };
    
    // Alphabetically sort the words.
    IEnumerable<string> query = from word in someWords
                                orderby word
                                select word;
    
  2. The moreWords variable isn't currently being used, but you might use it later, so instead of deleting it, you can comment out those lines. Select the entire definition of moreWords down to the closing semicolon, and then select the Comment out the selected lines button on the Text Editor toolbar, or select Ctrl+K, Ctrl+C.

    Screenshot of the Comment out button in the Text Editor toolbar in Visual Studio 2022.

    The C# comment characters // are added to the beginning of each selected line to comment out the code.

    To uncomment lines, you can select them, and then select Uncomment the selected lines button on the Text Editor toolbar, or select Ctrl+K, Ctrl+U.

    Screenshot of the Uncomment button in the Text Editor toolbar in Visual Studio 2022.

Collapse code blocks

If you don't want to see the empty constructor that was generated for Class1, you can collapse it to unclutter your view of the code. Select the caret sign in the margin of the first line of the constructor. Or, if you prefer to use the keyboard, place the cursor anywhere in the constructor code and select the Ctrl+M, Ctrl+M keys.

Screenshot of the Outlining collapse button in the Text Editor toolbar.

The code block collapses to just the first line, followed by an ellipsis (...). To expand the code block again, select the caret sign (>), or select 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

The Visual Studio editor makes it easy to inspect the definition of a type, method, or variable. One way is to go to the definition, in whichever file has it, by choosing Go to Definition or by selecting the F12 key anywhere a symbol is referenced. An even quicker way that doesn't move your focus away from the code you're working on is to use Peek Definition.

Let's peek at the definition of the string type.

  1. Right-click on any occurrence of string and choose Peek Definition from the content menu. Or, select the Alt+F12 keys.

    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.

    Screenshot of the Peek definition window in Visual Studio.

  2. Close the peek definition window by choosing the small box with an "x" at the top right of the pop-up window.

Use IntelliSense to complete words

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. Below the query variable, start typing the following code:

    foreach (string str in qu
    

    You'll see an IntelliSense pop-up appear with information about the query symbol.

    Screenshot of an IntelliSense word completion pop-up in Visual Studio 2022.

    If you're signed in with a GitHub account that has a Copilot subscription, you'll also see code suggestions appear in gray text.

  2. To insert the rest of the word query by using IntelliSense word completion, select the Tab key.

  3. Finish off the code block to look like the following code. You can practice further with code snippets by entering cw and then selecting Tab twice to generate the Console.WriteLine statement.

    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. Let's try out Visual Studio's refactor functionality to rename the someWords variable to unsortedWords.

  1. Place your cursor over the definition of the someWords variable, and choose Rename from the right-click or context menu, or select the F2 key.

    A Rename dialog box appears at the top right of the editor.

    Screenshot of the Rename pop-up box within the editor of Visual Studio 2022.

  2. Enter the desired name unsortedWords. You'll see that the reference to unsortedWords in the query assignment statement is also automatically renamed. Before you select the Enter key, select the Include comments checkbox in the Rename pop-up box.

    Screenshot of the Rename pop-up box in Visual Studio 2022.

  3. Select the Enter key.

    Both occurrences of someWords in your code have been renamed, as well as the text someWords in your code comment.

Next steps

See also