How to Write Boilerplate Code Using GitHub Copilot

Introduction

Boilerplate code is common in software development, especially when starting a new project. Writing repetitive code takes time and effort. GitHub Copilot helps by generating this code automatically, so developers can focus on building their applications.

In this article, we'll create a simple C# application using GitHub Copilot without manually typing a single line of code.

Step 1. Create a New Project

Start by creating a C# Console App in Visual Studio and deleting the automatically generated code. This will give us a clean slate to work with.

Step 2. Open GitHub Copilot Chat

  • Right-click in the editor and select Ask Copilot.
    Copilot Chat
  • Alternatively, use the shortcut Alt + / to open the Copilot chat window.
    Chat Window

Here, you can ask programming-related questions or paste existing code to get suggestions.

Note. Copilot only assists with programming-related queries. If you ask non-programming-related questions, it will inform you that such queries are not supported.

Step 3. Generate a Simple C# Program Using Copilot

Let’s ask Copilot to generate a simple program that,

  • Reads a text file.
  • Counts the number of characters and words in it.
  • Displays the results on the console.

Open Copilot chat type the following prompt and hit ENTER.

Hit Enter

Wait and see if the magic happens.

Copilot goes around the data it has, finds the code and writes the following code for you and ask you to review and accept it.

Data

Hit the Accept button.

This is what my complete program looks like,

using System;
using System.IO;

class Program
{
    static void Main()
    {
        string filePath = "path/to/your/file.txt";
        int characterCount = 0;
        int wordCount = 0;

        try
        {
            using (StreamReader sr = new StreamReader(filePath))
            {
                string line;
                while ((line = sr.ReadLine()) != null)
                {
                    characterCount += line.Length;
                    wordCount += line.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries).Length;
                }
            }

            Console.WriteLine("Total number of characters: " + characterCount);
            Console.WriteLine("Total number of words: " + wordCount);
        }
        catch (FileNotFoundException)
        {
            Console.WriteLine("File not found.");
        }
        catch (Exception ex)
        {
            Console.WriteLine("An error occurred: " + ex.Message);
        }
    }
}

Step 4. Review and Modify the Code

Keep in mind, that Copilot only writes code based on the data it has. You must review the code to make sure it works the way it should be and it does what it was supposed to.

To run this program, I need to change the following line to the correct text file with its full path.

string filePath = "path/to/your/file.txt";

I want to read an existing file named CSharpCorner.txt. I update the code with the following:

string filePath = "C:/temp/CSharpCorner.txt";

Step 5. Build and Run the Project

Now, build and run your project. The output should display the total number of characters and words in the specified text file.

Console

Code Review

Remember, Copilot is your AI programming assistant. You’re still in the driver’s seat. You must ensure that the code is written by Copilot, follows your standards, quality, and works the way it is supposed to.

Copilot is also likely to make mistakes since it just writes based on what data it has. It is your responsibility to do a code review.

Copilot may also use an older version of a programming language or framework. For example, if the current version of C# is 13, Copilot may write code in C# 11 or 12.

You want to make sure the code is written in the latest version of C#.

I asked Copilot to review the code and update it to C# 12.

Review

Copilot replies the following.

Review 1

The explanation of the code is below the code.

Code

The Preview option will show the updated code and changes line by line.

Changes

To accept the changes, click on the Accept button.

The changes will be reflected in your code file.

Build and run the project. You will get the same results.

Conclusion

GitHub Copilot is a game-changer for developers, helping automate repetitive tasks and improving productivity. However, it’s essential to review and validate the generated code to ensure quality and security. Try Copilot in your next project and experience the efficiency it brings to coding.

Download Copilot eBook: Copilot Handbook for Students and Developers

Up Next
    Ebook Download
    View all
    Learn
    View all
    Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.