Loading...

Using GitHub Copilot Chat in Visual Studio

Image

GitHub Copilot is a coding assistant powered by Artificial Intelligence (AI), which can run in various environments and help you be more efficient in your daily coding tasks. In this new series of content, we will show you how GitHub Copilot works in Visual Studio specifically and how it helps you being more productive. I just published the third short video in this series, titled "Using GitHub Copilot Chat in Visual Studio". The video is embedded below:

 

 

Learning about code with GitHub Copilot Chat

In the video, my colleague Bruno Capuano shows sample code filling an array with random numbers. Of course arrays are cool, but the C# language is much richer and offers other classes that one can use to create lists, with various functionalities. Copilot Chat is a great companion for this kind of explorations.

 

GitHub Copilot Chat can, for example, explain the difference between arrays and Lists. The differences listed in the example mention:

  • The size (an array is a fixed-size structure while a List can grow and shrink at runtime).
  • The performance, a very important consideration (arrays are generally more efficient than Lists because they don't have the burden of dynamic sizing).
  • The functionality (a List has more methods than an array, allowing for data manipulation)

In addition, sample code is also added to the chat, illustrating the creation of an array and a List, and adding one element.

 

Note that the user didn't have to mention using C# and .NET in the Copilot chat. This context was inferred from the solution and the file currently open. This is quite similar to a conversation that you would have with a fellow coder during pair-programming.

 

Converting code

In the next example, Bruno asks Copilot Chat to convert his code, initially using arrays, to use Lists instead. A very neat feature which I personally like a lot is the preview feature. Simply click the Preview button, and the code will be shown inline in the code file, but in a very neat "diff" view, similar to what you see when visualizing differences in code, for example after a commit in GitHub.

 

For me, it's a great way to see exactly what code will be changed, and most importantly, to review the code created by Copilot before accepting the changes. As we always say in this series, this is a Copilot, but you are the Pilot in Command!

 

I reproduced the sample that Bruno is showing, and here is the before-and-after code that Copilot Chat proposed to me:

 

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

 

 

LBugnion_0-1711442743928.png

Asking Copilot to convert the code

 

LBugnion_1-1711442743929.png

The proposed solution

 

LBugnion_2-1711442743930.png

The Preview feature

 

Keep learning!

Even at my "advanced career stage" (after all I've been working with C# and .NET since the early 2000s when it came out), I am still learning every day and GitHub Copilot is a great companion to show me new C# features and new Visual Studio functionalities. I used to love IntelliSense, and still do, but GitHub Copilot is boosting my code, and teaching me new syntax.

 

For more information, check our collection with resources here. Also, you can see the full length video here. Stay tuned to this blog for more content posted regularly. And of course, you can also subscribe to our YouTube channel to get more video content!

Learn more
Author image

Azure Developer Community Blog articles

Azure Developer Community Blog articles

Share post:

Related

Stay up to date with latest Microsoft Dynamics 365 and Power Platform news!

* Yes, I agree to the privacy policy