How to use GitHub Copilot Chat in Visual Studio

Cynthia Zanoni

Laurent Bugnion

A step-by-step guide to use GitHub Copilot Chat in Visual Studio
A step-by-step guide to use GitHub Copilot Chat in Visual Studio

GitHub Copilot serves as an AI-powered coding assistant capable of operating across various development environments, assisting you with your everyday coding challenges. In this new series of content, we will demonstrate how GitHub Copilot functions in Visual Studio especially and how it boosts your efficiency.

We just published “Using GitHub Copilot Chat in Visual Studio”, the third short video in this series, to guide you through leveraging GitHub Copilot Chat effectively within Visual Studio.

Exploring code with GitHub Copilot Chat

In the video, my coworker Bruno Capuano demonstrates how to populate an array with random numbers. Arrays are nice, but C# has more to offer and has other classes that can be used to make lists, with different features. GitHub Copilot Chat is a useful tool for this kind of investigations.

Make sure to have GitHub Copilot installed, refer to the documentation to learn how to install GitHub Copilot Chat for Visual Studio.

GitHub Copilot Chat can help you understand the difference between arrays and Lists. For example, it can list some of the differences, such as:

  • The size (an array has a fixed-size structure while a List can change its size at runtime).
  • The performance, a very important factor (arrays are usually more efficient than Lists because they don’t have to deal with dynamic sizing).
  • The functionality (a List has more methods than an array, allowing for data manipulation)

Also, it can show you some code examples that show how to create an array and a List, and how to add one element.

You don’t need to tell Copilot chat that you are using C# and .NET. It can figure that out from the solution and the file you are working on. This is similar to how you would talk to another coder when you are pair-programming.

Converting code

In the following example, Bruno wants Copilot Chat to change his code, which uses arrays at first, to use Lists instead. A very handy feature that I personally find very useful is the preview feature. You just need to click the Preview button, and the code will be displayed inline in the code file, in a nice “diff” view that looks like what you see when comparing code changes, for example after a GitHub commit.

I think it’s a great way to see exactly what code will be modified, and most importantly, to check the code generated by Copilot before applying the changes. As we always remind you in this series, this is a Copilot, but you are the Pilot in Command!

I followed the example that Bruno demonstrated, and here is the code that Copilot Chat suggested to me before and after the changes:

Before

// fill an array with 10 random numbers
int[] numbers = new int[10];
Random random = new Random();
for (int i = 0; i < numbers.Length; i++)
{
    numbers[i] = random.Next(1, 100);
}

After

// fill a list with 10 random numbers
List<int> numbers = new List<int>();
Random random = new Random();
for (int i = 0; i < 10; i++)
{
    numbers.Add(random.Next(1, 100));
}

Asking Copilot to convert the code
Asking Copilot to convert the code

 

The proposed solution
The proposed solution

 

The Preview feature
The Preview feature

Conclusion

Now that you have successfully installed GitHub Copilot in Visual Studio, you can now enjoy the benefits of AI-powered coding assistance. GitHub Copilot can help you write code faster and you can also learn from the suggestions and examples that GitHub Copilot provides. To learn more about GitHub Copilot and how to use it, check our collection with resources here or via our full-length video.

Additional Resources

Feedback usabilla icon