Skip to main content

Generative AI

C# + GitHub Copilot in Visual Studio 2022

Istock 2222587923

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 RepositoryEnsure 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.

Githublogin

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

  1. Click the Copilot status icon in the Visual Studio toolbar,
  2. From the dropdown menu, choose Sign in.
  3. Sign in with a GitHub account that has an active Copilot subscription.

Or…

  1. Select Open Chat Window
  2. 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

  1. Generating Simple Code with Copilot

    Step 1: Add a comment, e.g.

    Plain text
    Copy to clipboard
    Open code in new window
    EnlighterJS 3 Syntax Highlighter
    // Prompt: Write a method to calculate the factorial of a number using recursion
    // Prompt: Write a method to calculate the factorial of a number using recursion
    // 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#

    copilot with c#

    You can even prompt GitHub Copilot to generate unit tests:

    Plain text
    Copy to clipboard
    Open code in new window
    EnlighterJS 3 Syntax Highlighter
    // Unit test for Factorial method using MSTest
    // Unit test for Factorial method using MSTest
    // Unit test for Factorial method using MSTest

    GitHub Copilot will suggest

    Plain text
    Copy to clipboard
    Open code in new window
    EnlighterJS 3 Syntax Highlighter
    [TestMethod]
    public voidTestFactorial()
    {
    var calc = newCalculator();
    Assert.AreEqual(120, calc.Factorial(5));
    }
    [TestMethod] public void TestFactorial() { var calc = new Calculator(); Assert.AreEqual(120, calc.Factorial(5)); }
    [TestMethod]
    public void TestFactorial()
    {
        var calc = new Calculator();
        Assert.AreEqual(120, calc.Factorial(5));
    }
  2. Generating Complex Code with Copilot

    If we need the list of customers from the database with some filters, then we can prompt like

    Plain text
    Copy to clipboard
    Open code in new window
    EnlighterJS 3 Syntax Highlighter
    // Method to get active customers from a specific city using DbContext
    // Method to get active customers from a specific city using DbContext
    // Method to get active customers from a specific city using DbContext

    GitHub Copilot will generate like below sample code

    Plain text
    Copy to clipboard
    Open code in new window
    EnlighterJS 3 Syntax Highlighter
    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();
    }
    }
    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(); } }
    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.

Reference:  https://learn.microsoft.com/en-us/visualstudio/ide/visual-studio-github-copilot-extension?view=vs-2022

Thoughts on “C# + GitHub Copilot in Visual Studio 2022”

  1. Thank you, Vishal, for sharing your expertise and helping the community get more out of Copilot in the C# ecosystem.
    Keep up the great work!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Vishal Chikhalikar, Senior Technical Consultant

Vishal is a senior technical consultant here at Perficient. He has more than ten years of experience with .Net applications like SharePoint, Optimizely CMS and InRiver PIM. Vishal is always eager to pick up new skills and apply them to his projects.

More from this Author

Follow Us