Introduction
GitHub Copilot is an AI tool that helps developers write code quickly. It suggests full lines, functions, and even entire classes based on what you type. This makes coding faster, reduces mistakes, and improves productivity.
One of Copilot’s main features is code completion. It can automatically suggest code snippets and blocks as you type, making it easier to extend existing code.
Let’s try this
- I just type File in the code editor, you will see the entire line of code is already there. To accept this suggested code, all I must do is hit TAB.
![File]()
- After I hit the TAB key, the next line of the code is already suggested to me.
![Tab Key]()
Extend our application
- Let’s try to add a method to the existing class we created earlier in Listing 1. As soon as I type ‘public’, you will see Copilot suggest the following method WriteToFile.
![Suggestion]()
- But I really don’t want to write the above method. But I want to create a method that reads a text file line by line.
- As soon as I continue the method signature and type ReadLineB. Copilot automatically completes my intentions with the method ReadLineByLine as you can see below.
![ReadLineB]()
- Hit TAB to accept the new method.
- I hit ENTER below that and the new method suggested by Copilot is the following.
![New Method]()
- I hit ENTER again and the next suggested method is the following.
![ENTER again]()
In a similar fashion, you may continue to use Copilot to autocomplete code.
Extend existing program using Copilot
- Now, let’s extend our program. We are going to add a new class in the program and then add some objects to the text file.
class Author
{
public string Name { get; set; }
public string Email { get; set; }
public string Country { get; set; }
}
public static void AddAuthorToFile(Author author, string filePath)
{
using (StreamWriter sw = new StreamWriter(filePath, true))
{
sw.WriteLine($"Name: {author.Name}");
sw.WriteLine($"Email: {author.Email}");
sw.WriteLine($"Country: {author.Country}");
sw.WriteLine();
}
}
// 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
AddAuthorToFile(filePath, author);
Now build and run the application.
The console will ask you to enter your Name, Email, and Country input values. Provide these values and hit ENTER.
![Console]()
Now check the .txt file and you will see the new values are added to the text file.
![Output]()
Conclusion
GitHub Copilot simplifies coding by suggesting lines, methods, and classes as you type, making development faster and more efficient. It helps with code completion, method suggestions, and extending programs. By using Copilot, developers can save time, reduce errors, and improve productivity effortlessly.
Download Copilot eBook: Copilot Handbook for Students and Developers