Školenie
Modul
Guided project - Build auto suggest engine with Copilot - Training
Use GitHub Copilot to interpret, debug, and extend project code for a Trie data structure that supports autocomplete and spelling suggestion features.
Tento prehliadač už nie je podporovaný.
Inovujte na Microsoft Edge a využívajte najnovšie funkcie, aktualizácie zabezpečenia a technickú podporu.
In this 10-minute introduction to the code editor in Visual Studio, we'll add code to a file to look at some of the ways that Visual Studio makes writing, navigating, and understanding code easier.
Prepitné
If you don't have Visual Studio, go to Visual Studio downloads to install it for free.
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++.
Prepitné
To follow along with this article, make sure you have the C# settings selected for Visual Studio. For information about selecting settings for the integrated development environment (IDE), see Select environment settings.
Start by creating a new file and adding some code to it.
Open Visual Studio. Press Esc or select Continue without code on the start window to open the development environment.
From the File menu on the menu bar, select New > File.
In the New File dialog box, under the General category, select Visual C# Class, and then select Open.
A new file opens in the editor with the skeleton of a C# class. (Notice that we don't have to create a full Visual Studio project to gain some of the benefits that the code editor offers; all you need is a code file!)
Open Visual Studio. Select the Esc key, or select Continue without code on the start window, to open the development environment.
From the File menu on the menu bar, select New > File, or select the Ctrl+N keys.
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 the skeleton of a C# class.
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:
// Add a method to add two numbers
and Enter.Let's use Copilot Chat to submit a coding-related question as a prompt:
Select the GitHub Copilot badge in the upper-right corner of the IDE.
Select Open Chat Window from the dropdown.
Enter the following prompt in the chat window:
Generate sample code for a simple C# method to add two numbers.
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. Note that it requires Visual Studio 2022 version 17.8 or later.
Visual Studio provides useful code snippets that you can use to quickly and easily generate commonly used code blocks. Code snippets are available for different programming languages including C#, Visual Basic, and C++.
Let's add the C# void Main
snippet to our file.
Place your cursor just above the final closing brace } in the file, and type the characters svm
. (svm
stands for static void Main
; the Main() method is the entry point for C# applications.)
A pop-up dialog box appears with information about the svm
code snippet.
Press Tab twice to insert the code snippet.
You'll see the static void Main()
method signature get added to the file.
The available code snippets vary for different programming languages. You can look at the available code snippets for your language by choosing Edit > IntelliSense > Insert Snippet, and then choosing your language's folder. For C#, the list looks like this:
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.
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 for different programming languages. You can look at the available code snippets for your language by choosing Edit > IntelliSense > Insert Snippet or by selecting the Ctrl+K, Ctrl+X keys, and then choosing the folder for your programming language. For C#, the snippet list looks like this:
The list includes snippets for creating a class, a constructor, a for loop, an if or switch statement, and more.
The toolbar, which is the row of buttons under the menu bar in Visual Studio, can help make you more productive as you code. 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.
Paste the following code into the Main()
method body.
// _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;
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. Select the entire definition of morewords
to the closing semi-colon, and then choose the Comment out the selected lines button on the toolbar. If you prefer to use the keyboard, press Ctrl+K, Ctrl+C.
The C# comment characters //
are added to the beginning of each selected line to comment out the code.
The Text Editor toolbar, which is 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 that you don't want to compile.
Let's comment out some code.
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;
We're not using the moreWords
variable, but we might use it later so we don't want to delete it. Instead, we'll comment out those lines. Select the entire definition of moreWords
down to the closing semicolon, and then choose the Comment out the selected lines button on the Text Editor toolbar. If you prefer to use the keyboard, select Ctrl+K, Ctrl+C.
The C# comment characters //
are added to the beginning of each selected line to comment out the code.
When you want to uncomment lines, you can select them, and then choose the Uncomment the selected lines button on the Text Editor toolbar. If you prefer to use the keyboard, select Ctrl+K, Ctrl+U.
We don't want to see the empty constructor that was generated for Class1
, 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. Or, if you prefer to use the keyboard, 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 (...
). 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.
We don't want to see the empty constructor that was generated for Class1
, 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. Or, if you prefer to use the keyboard, place the cursor anywhere in the constructor code and select the Ctrl+M, Ctrl+M keys.
The code block collapses to just the first line, followed by an ellipsis (...
). To expand the code block again, select the same gray box that now has a plus sign in it, 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.
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. Let's peek at the definition of the string
type.
Right-click on any occurrence of string
and choose Peek Definition from the content menu. Or, press Alt+F12.
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.
Close the peeked definition window by choosing the small box with an "x" at the top right of the pop-up window.
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.
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.
Close the peek definition window by choosing the small box with an "x" at the top right of the pop-up window.
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.
Below the query
variable, start typing the following code:
foreach (string str in qu
You'll see IntelliSense show you Quick Info about the query
symbol.
To insert the rest of the word query
by using IntelliSense's word completion functionality, press Tab.
Finish off the code block to look like the following code. You can even practice using code snippets again by entering cw
and then pressing Tab twice to generate the Console.WriteLine
code.
foreach (string str in query)
{
Console.WriteLine(str);
}
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.
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.
To insert the rest of the word query
by using IntelliSense word completion, select the Tab key.
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);
}
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 _words
variable to words
.
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.
Enter the desired name words. Notice that the reference to words
in the query is also automatically renamed. Before you press Enter, select the Include comments checkbox in the Rename pop-up box.
Press Enter.
Both occurrences of words
have been renamed, as well as the reference to words
in the code comment.
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
.
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.
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.
Select the Enter key.
Both occurrences of someWords
in your code have been renamed, as well as the text someWords
in your code comment.
Školenie
Modul
Guided project - Build auto suggest engine with Copilot - Training
Use GitHub Copilot to interpret, debug, and extend project code for a Trie data structure that supports autocomplete and spelling suggestion features.
Dokumentácia
Learn about some of the windows, menus, and other UI features of the Visual Studio integrated development environment (IDE).
Explore advanced features for developers - Visual Studio (Windows)
Learn about the advanced features that might be more appropriate for experienced developers, or those developers who are already familiar with Visual Studio.
Visual Studio IDE documentation
Explore documentation about the features of the Visual Studio integrated development environment (IDE) that make it easy to edit, debug, build, and publish your app.