Skip to content

Boosting Productivity with GitHub Copilot Real-World .NET Coding Examples

Software development in the .NET ecosystem is powerful, but projects can quickly get bogged down with boilerplate code, repetitive tasks, and lengthy setup. This is where GitHub Copilot comes in. Acting as your AI pair programmer inside Visual Studio or VS Code, Copilot suggests context-aware code snippets, tests, and even full method implementations.

Let’s dive into real-world .NET coding examples that highlight how Copilot can make you more productive.

1. Quickly Generating ASP.NET Core Controllers

Building REST APIs in ASP.NET Core often involves repetitive patterns. Copilot can generate these structures with minimal typing.

Example: A Basic Products Controller

using Microsoft.AspNetCore.Mvc;

[ApiController]
[Route("api/[controller]")]
public class ProductsController : ControllerBase
{
    private readonly IProductService _productService;

    public ProductsController(IProductService productService)
    {
        _productService = productService;
    }

    [HttpGet]
    public IActionResult GetAll()
    {
        return Ok(_productService.GetAllProducts());
    }

    [HttpGet("{id}")]
    public IActionResult GetById(int id)
    {
        var product = _productService.GetProductById(id);
        if (product == null) return NotFound();
        return Ok(product);
    }
}

Copilot can scaffold out controllers with routes, parameter handling, and IActionResult responses, saving you from rewriting the same code across multiple controllers.

2. Entity Framework Data Models

Defining data models with Entity Framework involves repetitive property definitions. Copilot can infer these patterns based on your naming conventions.

Example: Product Entity

public class Product
{
    public int Id { get; set; }
    public string Name { get; set; } = string.Empty;
    public decimal Price { get; set; }
    public int Stock { get; set; }
    public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
}

As soon as you start typing public class Product, Copilot can suggest standard fields like Id, Name, and Price, accelerating your model design.

3. Writing Unit Tests Faster with xUnit

Testing in .NET is crucial but can be repetitive. Copilot can generate common test cases quickly.

Example: Unit Tests for ProductService

using Xunit;

public class ProductServiceTests
{
    [Fact]
    public void AddProduct_ShouldIncreaseCount()
    {
        var service = new ProductService();
        var initialCount = service.GetAllProducts().Count;

        service.AddProduct(new Product { Name = "Test", Price = 10.0m });

        Assert.Equal(initialCount + 1, service.GetAllProducts().Count);
    }

    [Fact]
    public void GetProductById_ShouldReturnNull_WhenNotFound()
    {
        var service = new ProductService();
        var result = service.GetProductById(999);
        Assert.Null(result);
    }
}

Instead of manually writing boilerplate test cases, Copilot can generate meaningful Arrange-Act-Assert style tests, which you can refine.

4. Auto-Generating LINQ Queries

Writing LINQ can be verbose, but Copilot often suggests correct query syntax once it understands your intent.

Example: LINQ Filtering

var expensiveProducts = products
    .Where(p => p.Price > 100)
    .OrderByDescending(p => p.Price)
    .ToList();

Typing var expensiveProducts = products. often prompts Copilot to complete the filtering logic, saving mental overhead.

5. Documentation and XML Comments

Copilot can generate XML doc comments for methods, which improves maintainability and IntelliSense support.

Example: Auto-Generated Doc Comment

/// <summary>
/// Calculates the discount price for a product.
/// </summary>
/// <param name="price">The original price.</param>
/// <param name="percentage">The discount percentage.</param>
/// <returns>The price after discount.</returns>
public decimal CalculateDiscount(decimal price, decimal percentage)
{
    return price - (price * (percentage / 100));
}

Instead of writing XML comments manually, start typing /// and Copilot will suggest structured documentation.

Best Practices for Using Copilot in .NET

  • Validate suggestions: AI is helpful, but always check for correctness and performance implications.
  • Use Copilot for scaffolding: Great for controllers, services, tests, and boilerplate—but refine logic yourself.
  • Pair it with .NET tooling: Combine Copilot with tools like Entity Framework migrations and ASP.NET scaffolding for maximum productivity.

GitHub Copilot can be a powerful partner in .NET development, helping you focus on solving business problems instead of rewriting boilerplate code. From ASP.NET Core controllers to Entity Framework models and xUnit tests, Copilot streamlines repetitive coding tasks, leaving you more time for creativity and innovation.

If you haven’t yet, give Copilot a try in your .NET workflow—it might just become your favorite coding assistant.