How to use GitHub Copilot Chat in Visual Studio

Cynthia Zanoni

Laurent Bugnion

In this series, we will explore the capabilities and how to use GitHub Copilot Chat within Visual Studio, demonstrating how it can elevate your coding efficiency. GitHub Copilot serves as an AI-powered coding assistant designed to enhance your coding experience across various development environments.

Exploring code with GitHub Copilot Chat

In our latest video, my colleague Bruno Capuano showcases the utility of GitHub Copilot Chat within Visual Studio, specifically focusing on populating arrays with random numbers. While arrays are a fundamental data structure, GitHub Copilot Chat facilitates exploration beyond arrays, guiding users towards utilizing more versatile structures like Lists.

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 streamlines the understanding of differences between arrays and Lists, highlighting key distinctions such as:

  • Size: Arrays have a fixed-size structure, whereas Lists can dynamically adjust their size during runtime.
  • Performance: Arrays generally offer higher efficiency due to their static sizing, while Lists excel in scenarios requiring frequent size adjustments.
  • Functionality: Lists boast a broader array of methods compared to arrays, enabling more versatile data manipulation.

Additionally, GitHub Copilot Chat offers code examples demonstrating the creation of arrays and Lists, alongside basic element addition operations.

You need not explicitly specify the programming language or framework to GitHub Copilot Chat; it intuitively adapts to the context of your solution and file, mimicking the natural flow of communication during pair-programming sessions.

Converting code

In a practical demonstration, Bruno utilizes GitHub Copilot Chat to seamlessly transition code initially utilizing arrays into more flexible List-based implementations. Notably, the preview feature provides a comprehensive overview of proposed code modifications, empowering users to validate Copilot-generated solutions before implementation.

I’ve replicated Bruno’s example below, showcasing the code suggested by Copilot Chat both before and after the conversion:

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);
}

When selecting the code and opening the GitHub Copilot chat dialogue window, we’ll ask Copilot to convert the code to use the list format.

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

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));
}

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