Install, configure, and test GitHub Copilot in Visual Studio 2022

If you have not read or heard about Microsoft Copilot read more about it here.  In summary it is a natural language generative AI application that can respond to questions using your proprietary/private data in sentences and paragraphs instead of links.  It does much more than that, read the article if you want to find out more.  In regards to GitHub Copilot which is a tool focused on optimizing the development of code, the more you use it the more you will learn of it capabilities and the manner in which your development routine is optimized and will truly become more fun, pleasurable, and productive.

As seen in Figure 1, you must first install the GitHub Copilot Extension. 

image

Figure 1, install the GitHub Copilot Visual Studio 2022 extension

Be sure to close down Visual Studio so that the installation can begin.  During the installation time, you will see the VSIX Installer window, Figure 2, rendering a status as it installs and completes.

image

Figure 2, VSIX installation of GitHub Copilot in Visual Studio 2022

Once the installation is complete, open Visual Studio 2022 and you should be prompted with this banner, as seen in Figure 3.  The banner states [Copilot] To enable GitHub Copilot, add your GitHub account to Visual Studio.

image

Figure 3, [Copilot] To enable GitHub Copilot, add your GitHub account to Visual Studio

If the window shown in Figure 4 does not open or if you do not see the banner, you can click on your image in the top right of the IDE and click the Account Settings link.  Click +Add, Select GitHub and add your account by providing the credentials and authorizing its access.

image

Figure 4, add, configure GitHub to Visual Studio 2022 to enable GitHub Copilot

Whether starting a new project, modifying or updating an existing project, an interesting AI / Copilot ability can be seen in Figure 5.  GitHub Copilot is able to take the name of my class and suggest attribute and method names which it considers valid.  I thought this was kind of funny so I left it.

image

Figure 5, test GitHub Copilot in Visual Studio 2022

In all seriousness, when I begin programming an application I am not given a design document that lists out all the necessary classes, methods and attributes, this is something I need to come up with and create as part of the structure that will support the business logic.  GitHub Copilot can guide me through this as long as I am able to name the classes in such a way which make their purpose intuitive.  This in turn makes the support of the code better as well because understanding what the code is doing is easier if the names used to execute it describe its purpose.

I named a class CalcualteAreaOfCircle, DecompressFile, etc… and the code to do so was written for me.  For example, writing a Regular Expression to remove special characters from a string.  I simply added this at the top of the Program.cs file and wrote public below it.

//remove special characters from a string
public

The method which was generated for me is shown here, this took less than a single second to write this.

public static string RemoveSpecialCharacters(string str)
{
     return Regex.Replace(str, "[^a-zA-Z0-9]", "", RegexOptions.Compiled);
}

Had I needed to do this myself it would have taken some minutes, multiply this by the other methods I need to write and you can begin to see how much more efficient I will be and how much faster I can complete the assignment.  Instead of looking through StackOverflow or example code, search through blog after blog, clicking on link after link, I get the example rendered immediately in Visual Studio.  All I need to know is what the requirements are.  One more example, so you get the point.  I entered only the test after the // and the code was written for me, wow.

//call a REST API that returns an account balance in JSON format
public static async Task<decimal> GetAccountBalance(string accountNumber)
{
     var client = new HttpClient();
     var response = await client.GetAsync($"https://endpoint/api/accounts/{accountNumber}");
     var json = await response.Content.ReadAsStringAsync();
     var account = JsonConvert.DeserializeObject<Account>(json);
     return account.Balance;
}

There are going to be a lot coming from this, this is not just hype, this is truly something real and will make a difference and change.

Helpful links



Leave a Comment

Your email address will not be published.