Adding GitHub Copilot Custom Instructions

GitHub Copilot is an AI agent that can assist developers in writing code. By simply typing a prompt in a natural language (like English), GitHub Copilot can generate code for you.

If you are using Visual Studio or Visual Studio Code, you can make GitHub Copilot even more powerful by adding Custom Instructions to your project. Custom instructions are sent with every prompt you enter. This is useful if you want to apply rules or guidance to all code in your project.

To take advantage of Copilot Custom Instructions, create a folder named ".github" in the root of your project and add a file name "copilot-instructions.md". In this markdown file, enter any natural language instructions you like. These instructions can refer to coding styles, naming conventions, or preferred frameworks and tools.

Below is an example of instructions in a "copilot-instructions.md" file:

Method names should be descriptive with mixed case and an underscore between words
Always use hungarian notation for variable names
Use 2 spaces for indentation
Use single quotes for strings
Use double quotes for docstrings
Prefix private class members with underscore (_)
Use ALL_CAPS for constants
For methods and classes, place the curly braces on a different line from the method or class name.

For error handling:
- Add try/catch blocks to all methods
- Use specific exceptions
- Avoid using generic exceptions
- Use custom exceptions for specific cases
- Log errors with a logger
- Always log errors with contextual information

Add triple slash comments at the top of each class and method. Include a brief description of the class or method, its parameters, and return values.

After saving the above file, I entered the following prompt:

Create a class to manage customers with CRUD methods. Allow passing in objects or properties

This prompt generated the following code. Note that the conventions in the code match those in my instructions file.

/// 
/// CustomerManager class manages customers with CRUD methods.
/// Methods accept Customer objects or individual properties.
/// 
public class CustomerManager
{
    private List _lstCustomers = new List();
    private const int NOT_FOUND = -1;

    /// 
    /// Add_Customer adds a new customer.
    /// Params: objCustomer or (nCustomerId, strName, strEmail)
    /// Returns: bool (success)
    /// 
    public bool Add_Customer(Customer objCustomer)
    {
        try
        {
            _lstCustomers.Add(objCustomer);
            return true;
        }
        catch (Exception ex)
        {
            Console.WriteLine('Add_Customer error: ' + ex.Message);
            return false;
        }
    }

    /// 
    /// Add_Customer adds a new customer by properties.
    /// 
    public bool Add_Customer(int nCustomerId, string strName, string strEmail)
    {
        try
        {
            var objCustomer = new Customer
            {
                nCustomerId = nCustomerId,
                strName = strName,
                strEmail = strEmail
            };
            return Add_Customer(objCustomer);
        }
        catch (Exception ex)
        {
            Console.WriteLine('Add_Customer(props) error: ' + ex.Message);
            return false;
        }
    }

    /// 
    /// Get_Customer retrieves a customer by id.
    /// Params: nCustomerId
    /// Returns: Customer or null
    /// 
    public Customer Get_Customer(int nCustomerId)
    {
        try
        {
            return _lstCustomers.FirstOrDefault(c => c.nCustomerId == nCustomerId);
        }
        catch (Exception ex)
        {
            Console.WriteLine('Get_Customer error: ' + ex.Message);
            return null;
        }
    }

    /// 
    /// Update_Customer updates a customer by object.
    /// Params: objCustomer
    /// Returns: bool (success)
    /// 
    public bool Update_Customer(Customer objCustomer)
    {
        try
        {
            int nIndex = _lstCustomers.FindIndex(c => c.nCustomerId == objCustomer.nCustomerId);
            if (nIndex == NOT_FOUND)
                return false;
            _lstCustomers[nIndex] = objCustomer;
            return true;
        }
        catch (Exception ex)
        {
            Console.WriteLine('Update_Customer error: ' + ex.Message);
            return false;
        }
    }

    /// 
    /// Update_Customer updates a customer by properties.
    /// 
    public bool Update_Customer(int nCustomerId, string strName, string strEmail)
    {
        try
        {
            var objCustomer = Get_Customer(nCustomerId);
            if (objCustomer == null)
                return false;
            objCustomer.strName = strName;
            objCustomer.strEmail = strEmail;
            return true;
        }
        catch (Exception ex)
        {
            Console.WriteLine('Update_Customer(props) error: ' + ex.Message);
            return false;
        }
    }

    /// 
    /// Delete_Customer removes a customer by id.
    /// Params: nCustomerId
    /// Returns: bool (success)
    /// 
    public bool Delete_Customer(int nCustomerId)
    {
        try
        {
            var objCustomer = Get_Customer(nCustomerId);
            if (objCustomer == null)
                return false;
            _lstCustomers.Remove(objCustomer);
            return true;
        }
        catch (Exception ex)
        {
            Console.WriteLine('Delete_Customer error: ' + ex.Message);
            return false;
        }
    }

    /// 
    /// Get_All_Customers returns all customers.
    /// Returns: List
    /// 
    public List Get_All_Customers()
    {
        try
        {
            return new List(_lstCustomers);
        }
        catch (Exception ex)
        {
            Console.WriteLine('Get_All_Customers error: ' + ex.Message);
            return new List();
        }
    }
}

Custom Instructions can increase your productivity when using GitHub Copilot. Try this on your next project.