Writing code comments and documentation is often overlooked by developers, yet it plays a crucial role in making code readable, maintainable, and easier to understand, especially for teams. Good documentation helps new developers get up to speed quickly and ensures that existing code remains easy to modify and debug.
With GitHub Copilot, developers can automate the process of adding comments and documentation, saving time and effort. This article explores how Copilot can assist in commenting on and documenting your code efficiently.
Commenting code using Copilot
You can use Copilot to add comments to your code by simply asking Copilot.
The following prompt adds comments to the Author class.

The following prompt comments entire program.

Now, the updated program is listed in Listing 2.
// Represents an author with properties for name, email, and country
class Author
{
public string Name { get; set; }
public string Email { get; set; }
public string Country { get; set; }
}
class Program
{
static void Main()
{
// Specify the file path
string filePath = "C:/temp/CSharpCorner.txt";
// Initialize character and word count variables
int characterCount = 0;
int wordCount = 0;
try
{
// Read the file line by line
using (StreamReader sr = new StreamReader(filePath))
{
string line;
while ((line = sr.ReadLine()) != null)
{
// Update character count
characterCount += line.Length;
// Split the line into words and update word count
wordCount += line.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries).Length;
}
}
// Display the total number of characters and words
Console.WriteLine($"Total number of characters: {characterCount}");
Console.WriteLine($"Total number of words: {wordCount}");
}
catch (FileNotFoundException)
{
// Handle file not found exception
Console.WriteLine("File not found.");
}
catch (Exception ex)
{
// Handle other exceptions
Console.WriteLine($"An error occurred: {ex.Message}");
}
// Accept input from the console
Console.WriteLine("Enter author details:");
Console.Write("Name: ");
string name = Console.ReadLine();
Console.Write("Email: ");
string email = Console.ReadLine();
Console.Write("Country: ");
string country = Console.ReadLine();
// Create an instance of the Author class
Author author = new Author
{
Name = name,
Email = email,
Country = country
};
// Call the AddAuthorToFile method to append author details to the file
AddAuthorToFile(filePath, author);
}
// Method to read the file line by line and display each line
public static void ReadLineByLine(string filePath)
{
using (StreamReader sr = new StreamReader(filePath))
{
string line;
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
}
// Method to read all lines from the file and display each line
public static void ReadAllLines(string filePath)
{
string[] lines = File.ReadAllLines(filePath);
foreach (string line in lines)
{
Console.WriteLine(line);
}
}
// Method to append author details to the file
public static void AddAuthorToFile(string filePath, Author author)
{
using (StreamWriter sw = new StreamWriter(filePath, true))
{
sw.WriteLine($"Name: {author.Name}");
sw.WriteLine($"Email: {author.Email}");
sw.WriteLine($"Country: {author.Country}");
sw.WriteLine();
}
}
}
Now only can you comment your existing code but can also ask to remove existing comments.
The following prompt removes all existing comments from the program.

Once you accept the changes, all comments from the code will be removed.



Join the conversation! Your thoughts help the community grow.