What is GitHub Copilot?
GitHub Copilot is an AI-powered programming assistant that assists developers by generating code based on their comments and existing code. Now natively integrated into Visual Studio 2022, it supports multiple languages, including C#.
Steps to Set Up GitHub Copilot in Visual Studio 2022
Prerequisites
- Visual Studio 2022 – Version 17.10 or later
- .NET Desktop Development workload installed
- GitHub Account – With an active Copilot subscription
- GitHub Copilot Extension – Installed via Visual Studio Installer
- Local Git Repository – Ensure your C# project is under Git version control
After installation, sign in with your GitHub account that has Copilot access. You’ll see a GitHub Copilot icon in the top-right corner of the IDE indicating its status.
Activating GitHub Copilot in Visual Studio
If GitHub Copilot is installed but in an inactive state, it may be because:
- You’re not yet signed into Visual Studio with a GitHub account
To Activate Copilot
- Click the Copilot status icon in the Visual Studio toolbar,
- From the dropdown menu, choose Sign in.
- Sign in with a GitHub account that has an active Copilot subscription.
- Don’t have a subscription? You can purchase one at: https://github.com/pricing
Or…
- Select Open Chat Window
- Choose:
- Sign up for Copilot Free to sign up for Copilot Free
- Sign in if you already have a Copilot-enabled account.
Once signed in, the Copilot status icon will change to indicate it’s active.
Using GitHub Copilot to Generate C# Methods
Generating Simple Code with Copilot
Step 1: Add a comment, e.g.
// Prompt: Write a method to calculate the factorial of a number using recursion
Step 2: GitHub Copilot will suggest code based on your comment after a few seconds.
Step 3: Accept or Modify
- Press Tab to accept.
- Use Alt +] or Alt + [ to cycle through suggestions.
- Modify as needed.
Please watch the video below which helps us to use GitHub Copilot in C#
You can even prompt GitHub Copilot to generate unit tests:
// Unit test for Factorial method using MSTest
GitHub Copilot will suggest
[TestMethod] public void TestFactorial() { var calc = new Calculator(); Assert.AreEqual(120, calc.Factorial(5)); }
Generating Complex Code with Copilot
If we need the list of customers from the database with some filters, then we can prompt like
// Method to get active customers from a specific city using DbContext
GitHub Copilot will generate like below sample code
public class CustomerService { private readonly AppDbContext _context; public CustomerService(AppDbContext context) { _context = context; } public async Task<List<Customer>> GetActiveCustomersByCityAsync(string cityName) { return await _context.Customers .Where(c => c.IsActive && c.City == cityName) .OrderBy(c => c.LastName) .ToListAsync(); } }
Benefits of Using GitHub Copilot
- Speeds up development by generating boilerplate and repetitive code.
- Understands your intent through comments and method names.
- Reduces manual errors in common data access patterns.
- Supports clean architecture by suggesting service-layer methods.
Things to Keep in Mind
- Always review the code for correctness and performance.
- Copilot doesn’t know your business logic, so suggestions may need tweaking.
- Security and validation are your responsibility.
- Suggestions can vary, so results may not always be consistent.
- Over-reliance can hinder learning if used without understanding the code.
Conclusion
For C# developers, GitHub Copilot in Visual Studio 2022 is revolutionary. It decreases complexity and increases productivity in everything from creating methods to writing tests. Experience the future of AI-assisted development by giving it a try.
Source: Read MoreÂ