In this article, you learn about Copilot Completions, an AI-powered pair programmer for Visual Studio that provides you with context-aware code completions, suggestions, and even entire code snippets. This powerful tool aids AI-assisted development in Visual Studio and helps developers write code more efficiently, reduce the time spent on repetitive tasks, and minimize errors.
GitHub Copilot works by utilizing advanced machine learning models trained on a vast dataset of publicly available code from GitHub repositories. As you type code, the AI analyzes the context and provides relevant suggestions in real-time. You can receive suggestions also by writing a natural language comment describing what you want the code to do.
You can also use GitHub Copilot to convert comments to code, create unit tests, create SQL queries, and more.
GitHub Copilot supports several programming languages and frameworks, including but not limited to: C#, C++, and Python.
Get GitHub Copilot for Visual Studio
If you're using Visual Studio version 17.10 or later, the unified GitHub Copilot extension is available as a recommended component in the Visual Studio Installer. It is installed by default with all workloads, unless you choose to exclude it during installation.
Using GitHub Copilot for code completions and suggestions
As you type code or comments in the editor, GitHub Copilot provides context-aware code completions and suggestions. GitHub Copilot provides suggestions for numerous languages, but works especially well for Python, JavaScript, TypeScript, Ruby, Go, C# and C++. The following examples are in C#, but other languages will work similarly.
Open Visual Studio and create a new C# project.
Open a C# file (Program.cs) in the editor.
In the code editor, type a comment or a method signature to see inline code suggestions from GitHub Copilot.
// method to add two numbers
int subtractNumbers(
Hover over the suggestion to see the GitHub Copilot command palette.
Accept suggestion
Select Tab or press Tabto accept the suggestion or continue typing to reject it.
Modify suggestion
Select Alt+/ or press Alt+/to modify the suggestion from Copilot. You can modify and curate the GitHub Copilot suggestions by adding context in inline chat or by editing the completion.
If the command palette doesn't appear, you might have reached the default limit for its display. To change this, go to Tools > Options > IntelliCode > Advanced, and adjust the value for Minimum commits to suppress hint text.
Add following code to see whole line completions from GitHub Copilot.