How to use GitHub Copilot like a Pro: Part 2

How to use GitHub Copilot like a Pro: Part 2

If you are looking for the part 1 series of using GitHub Copilot for Developers here it is: (14) How to use GitHub Copilot: A guide for developers | LinkedIn


Now earlier we learned how to setup GitHub copilot and make basic prompts to get simple code to run on our VS Code. Next we will dive deeper into how we can use the GitHub Copilot in your favorite IDE.

Developing .NET applications can involve repetitive tasks like creating boilerplate code, writing common functionalities, and implementing standard design patterns. GitHub Copilot, an AI code completion tool integrated with Visual Studio Code (VS Code), can significantly streamline this process by suggesting relevant code snippets and entire functions based on your context. This article dives deep into leveraging GitHub Copilot for end-to-end development of a .NET application in VS Code, covering project setup, code generation, running and testing the application.

Prerequisites:

  • An active GitHub account with a GitHub Copilot subscription (free trial available)
  • Visual Studio Code installed with the C# extension
  • Basic understanding of C# and .NET development

Setting Up the Project:

  • Create a New Project: Open VS Code and navigate to File > Open Folder. Choose a directory for your project and click Open.
  • Initialize .NET Project: Right-click anywhere in the open folder and select Terminal. In the terminal, run dotnet new console to create a basic .NET Console application.

'dotnet new console' creates a new console application

  • Install Copilot Extension: While Copilot is often bundled with the latest VS Code versions, if it's not automatically installed, go to the Extensions tab (Ctrl+Shift+X) and search for "GitHub Copilot". Install the official extension for enhanced AI assistance.

GH CoPilot is installed

Generating Code with Copilot:

  • Create Program.cs: Inside your project folder, create a new file named Program.cs. This will be the entry point for your application.
  • Write Comments: Copilot excels at understanding context. Start by writing a comment describing the desired functionality. For example: "create a stone, paper, scissor game"
  • Trigger Copilot: Place your cursor below the comment and press Ctrl + Enter. Copilot will analyze the comment and suggest relevant code.

Github Copilot generates the code for the application


  • Accept Suggestions: Review the generated code snippet. If it aligns with your needs, press Tab to accept and incorporate it into your code. You can further refine the suggestions by adding more comments or adjusting existing code.

Example:

// create a stone, paper, scissor game        

C#

// create a stone, paper, scissor game

// create a list of options
List<string> options = new List<string>() { "rock", "paper", "scissor" };

// create a random generator
Random random = new Random();

// get a random option from the list
string computerChoice = options[random.Next(0, options.Count)];

// get a user input
Console.WriteLine("Enter your choice: ");
string userChoice = Console.ReadLine();

// check if user choice is valid
if (!options.Contains(userChoice))
{
    Console.WriteLine("Invalid choice");
    return;
}

Console.WriteLine($"Computer choice: {computerChoice}");
Console.WriteLine($"User choice: {userChoice}");

// check who won
if (computerChoice == userChoice)
{
    Console.WriteLine("It's a draw");
}
else if (computerChoice == "rock" && userChoice == "scissor")
{
    Console.WriteLine("Computer won");
}
else if (computerChoice == "scissor" && userChoice == "rock")
{
    Console.WriteLine("You won");
}
else if (computerChoice == "paper" && userChoice == "rock")
{
    Console.WriteLine("Computer won");
}
else if (computerChoice == "rock" && userChoice == "paper")
{
    Console.WriteLine("You won");
}
else if (computerChoice == "scissor" && userChoice == "paper")
{
    Console.WriteLine("Computer won");
}
else if (computerChoice == "paper" && userChoice == "scissor")
{
    Console.WriteLine("You won");
}        

Building and Running the Application:

  • Open Terminal: Open the integrated terminal in VS Code (Terminal > New Terminal).
  • Navigate to Project Directory: Use the cd command to navigate to your project directory.
  • Build the Application: Run dotnet build to compile your code. This creates an executable file (e.g., Program.exe).


  • Run the Application: To run the application simply run 'dotnet run CSharp.csproj' or simply 'dotnet run' The program should prompt for your choices for the game and displays the results of the game.

Make sure that you win from the computer LOL!

So how cool was it to develop a .net C# Application game from the scratch using GitHub Copilot, no matter we still we had not squander any documentation or youtube video to get the basics done first. Its super easy and cool to get started building your first application using GitHub Copilot. Try around for your next project do ping me the github repository link for me to view, what did you create out of GitHub Copilot.

Soumyadip Majumder

ASP .NET Core | MAUI | Ex-Data Science trainee at Celebal Technologies | Ex-Database Designer @Dataiku (Remote)

11 个月

Very useful Post. Sourav Bera bhaiya

回复

要查看或添加评论,请登录

Sourav Bera的更多文章

社区洞察

其他会员也浏览了