This contains a simple demo for Copilot for C# and .NET through Visual Studio IDE. This is going to be useful for customers who are interested in using Copilot for C# and .NET and want to use Copilot to generate code through Visual Studio IDE.
Note that Copilot extension for Visual Studio IDE is only available for Windows. If you are using Mac, you can use Copilot through VS Code.
Welcome to GitHub Copilot! In this example, we'll show you how to use Copilot to write a simple .NET application.
- C#
- Visual Studio IDE for Windows
This guide will walk you through the steps to get started with Copilot in Visual Studio IDE.
Make sure that you meet the following requirements.
- Visual Studio IDE for Windows
- Copilot Extension for Visual Studio IDE.
See this documentation on how to install Copilot extension for Visual Studio IDE
Make sure that you followed all prerequisites and installed Visual Studio IDE and Copilot extension for Visual Studio IDE.
Search for Visual Studio after clicking Windows button.
Then, your Visual Studio IDE will be launching.
Once Visual Studio IDE is launched, you will see the following screen. Although you can use an existig project, let's try to create a new project by selecting Create a new project.
Then, you will see different options. Select Console App (.NET Framework) and click Next.
Then, you will see the following screen. Enter HelloCopilot as the project name and click Create.
You will see a progress bar while Visual Studio IDE is creating a new project.
Let's make sure that Copilot is enabled. On bottom, you should see Auth Status: OK under Output window. Make sure to select GitHub Copilot from the dropdown.
And you should also see a Copilot icon above the Output window.
Let's start writing some simple code first. Inside your Main method, create a following comment after //
// Print out Hello, Copilot 3,000 time with incrementing index
As soon as you enter next line, you should see that Copilot is suggesting you to write a code.
Select the first suggestion by hitting Tab button.
By the way, since this is a Console App, it will be a good idea to add the following line at the end of your Main method.
Console.ReadLine();
This can helps to keep the console window open after the program is finished.
Your file should look like this.
That is it for writing a very simple code using Copilot. We can run this code by clicking Start button on top, but let's actually write a function.
We will add a function above the Main method. Let's add a following comment as a block comment.
/*
* Function to sum all the numbers in a list of integers
*/
Again, when you enter, it should show you a suggestion. Yours might look different frome mine, though.
Hit Tab button to select the first suggestion. Your code might look like this.
Let's proceed to add codes to run that within main function. In the Main method, start clicking anywhere and click Enter button to see suggestions made by Copilot.
You can keep hit Enter and Tab buttons to take suggestions, but make sure that you can stop once you are happy with the result or fix if needed.
Now, let's run the code. Click Start button on top.
If there is a drop down, click Start again.
Then, you will a console window with the following output.
Let's add little more complicated function. Add a following comment as a block comment.
/*
* Function to randomly assign 4 digit codes to N x M matrix representing lockers
*/
Going back to Main function again, start hitting Enter and Tab buttons to take suggestions.
Sometime, you might need to enter more details result to correct.
Sometimes, you might encounter an error like this.
Although you may not get an exactly same result, this example has a problem with index because it was using i
as an index for both for
loops. Let's fix that by changing the second for
loop to use j
instead of i
.
Remember. Copilot is not perfect, and it can make some dumb mistakes. You might need to fix the code to make it work. You are the main pilot, and Copilot is your assistance.
Your final result when you run the code again might look like this.
That is it! Congratulation on finishing your first exercise with Copilot. You can try to write more complicated code and see how Copilot can help you.
Contributions are warmly welcomed! ✨
To contribute to a public exercise, please refer to our contribution guidelines here.
To create a net new exercise, please use this repository template.