Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 25 additions & 13 deletions Using-GitHub-Copilot-with-CSharp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ By the end of this module, you'll acquire the skills to be able to:

1. Enable your [GitHub Copilot service](https://github.com/github-copilot/signup)

1. Get familiar with [this repository with Codespaces](https://github.com/github/dotnet-codespaces)
1. Open [this repository with Codespaces](https://github.com/github/dotnet-codespaces)

## 💪🏽 Exercise

Expand Down Expand Up @@ -88,6 +88,14 @@ In the above exercises we achieved the following:

### 🗒️ Section 2: Code Completion

**🎯 Learning Goals**

- Use inline code completion to write code from comments.
- Trigger and refine Copilot completions.
- Use inline chat and slash commands.

In the previous section you learned how to use both natural language or slash commands to quickly understand the codebase without digging through folders. In the next set of exercises we're going to use Copilot to make some changes to the API using code completion.

1. Ask GitHub Copilot in the chat pane to "run and debug" the backend project (you can also do this from the 'run and debug' panel in the editor). Start Debugging the selected project.

<div align="left">
Expand All @@ -112,7 +120,7 @@ https://< your url>.app.github.dev/weatherforecast
<img src="./images/005ports.jpg" alt="port view in the editor" width="500" height="300">
</div>

1. In this next step we want to refactor some code and generate a new record for our Weather API. The goal is to add a new record that includes the name of the city. We are going to achieve this by asking Copilot directly from the file using code comments. From the file pane, navigate to the following path `SampleApp\BackEnd\Program.cs` and open `Program.cs`. Navigate to the end of the file and type in (or copy):
3. In this next step we want to refactor some code and generate a new record for our Weather API. The goal is to add a new record that includes the name of the city. We are going to achieve this by asking Copilot directly from the file using code comments. From the file pane, navigate to the following path `SampleApp\BackEnd\Program.cs` and open `Program.cs`. Navigate to the end of the file and type in (or copy):

```csharp
// create a new internal record named WeatherForecastByCity that requests the following parameters: City, Date, TemperatureC, Summary
Expand All @@ -124,19 +132,19 @@ The output should be similar to this:
<img src="./images/006internalrecord.jpg" alt="changes to existing record" width="500" height="300">
</div>

4. Now we want to use the inline feature of Copilot. Using the inline feature we can focus on the code in front of us and make progressive changes as needed in our code base. Next, we will create a new C# class by creating a new file under the `Backend` folder: `SampleApp\BackEnd\Customer.cs`.
1. Now we want to use the inline feature of Copilot. When using the inline feature we can focus on the code in front of us and make progressive changes as needed in our code base. Next, we will create a new C# class by creating a new file under the `Backend` folder: `SampleApp\BackEnd\Customer.cs`.
Copy link
Preview

Copilot AI Jul 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The numbering is inconsistent. This should be '4.' to continue the sequence from the previous numbered steps, not '1.'

Suggested change
1. Now we want to use the inline feature of Copilot. When using the inline feature we can focus on the code in front of us and make progressive changes as needed in our code base. Next, we will create a new C# class by creating a new file under the `Backend` folder: `SampleApp\BackEnd\Customer.cs`.
4. Now we want to use the inline feature of Copilot. When using the inline feature we can focus on the code in front of us and make progressive changes as needed in our code base. Next, we will create a new C# class by creating a new file under the `Backend` folder: `SampleApp\BackEnd\Customer.cs`.

Copilot uses AI. Check for mistakes.


<div align="left">
<img src="./images/007customerfile.png" alt="New file customer.cs" width="500" height="300">
</div>

From the top of the editor, press `CTRL + I` to open the inline chat and type (or copy) in:
From the top of the editor, press `CTRL + I` to open inline chat and type (or copy) in:

```csharp
// Create a class for a Customer with Id, Name, and Email, and a method to validate email format
```

5. Accept the suggestion and and with the output, using the inline prompt window ask:
5. Accept the suggestion and with the output, using the inline prompt window ask:

```csharp
/improve IsValidEmail method using Regex
Expand All @@ -154,7 +162,7 @@ Review the suggestions from Copilot and verify the output. **Note:** It is impor
**Note:** In the case that you do not see [Next Edit Suggestions](https://code.visualstudio.com/blogs/2025/02/12/next-edit-suggestions) appear in your editor, check that they are enabled in your editor.

<div align="left">
<img src="./images/NextEdit.gif" alt="Output of /Explain" width="500" height="300">
<img src="./images/NextEdit.gif" alt="Next Edit Feature" width="500" height="300">
</div>

In the above exercises we achieved the following:
Expand All @@ -171,10 +179,10 @@ In the above exercises we achieved the following:

[GitHub Copilot Agent Mode](https://github.blog/ai-and-ml/github-copilot/agent-mode-101-all-about-github-copilots-powerful-mode/) is an autonomous AI coding assistant that acts as a synchronous collaborator in real-time. It can perform coding tasks that require multiple steps all while using your natural language prompts. When working in Agent Mode, Copilot can problem solve alongside you, understanding your intent, and when the built solution needs some tweaking, it can iterate until it gets it right. In this section we're going to use Agent Mode to make a multitude of changes to our backend service to provide several improvement to our code.

1. Open GitHub Copilot Chat. Notice in the text box you can 'add context', which allows you to attach files, folders and other items to the context of Copilot so that it can better understand your codebase. Agent mode should be selected by default. You have the capability to choose your model as well. Also note, the open tab `Program.cs` is being referenced for context.
1. Open GitHub Copilot Chat. Notice in the text box you can 'add context', which allows you to attach files, folders and other items to the context of Copilot so that it can better understand your codebase. Agent mode should be selected by default. You have the capability to choose your model as well. Also note, the open tab `Program.cs` is being referenced to for context.

<div align="left">
<img src="./images/009agentview.jpg" alt="Output of /Explain" width="500" height="300">
<img src="./images/009agentview.jpg" alt="Output of /Explain">
</div>

2. Give Agent mode a detailed prompt. For this example we are going to ask Copilot to make several improvements to our project. Copy and paste the prompt below into the Chat window.
Expand All @@ -197,18 +205,18 @@ When executing in Agent mode, Copilot will take a bit longer to work through all
But where Agent mode really shines is that it will iterate on its own output until it resolves errors and reaches a working solution. As agent mode is running, we can see it is running tests to check and verify its own code:

<div align="left">
<img src="./images/agentmodetests.gif" alt="Output Agent Mode Implementation" width="500" height="300">
<img src="./images/agentmodetests.gif" alt="Output Agent Mode Tests">
</div>

While Agent Mode is working, we can also view that it has created documentation per our requirements outlined in the tasks that it was given:

<div align="left">
<img src="./images/011agentmodedocs.jpg" alt="Output Agent Mode Implementation" width="500" height="300">
<img src="./images/011agentmodedocs.jpg" alt="Output Agent Mode Documentation">
</div>

For this step in the workshop, agent mode should have taken several minutes to complete your tasks. Review the various changes and confirm that they have achieved all of the given output. With Agent mode, it also outputs what changes were made. The following summary of changes that were made are summarized below:

```
```md
**Customer Endpoint Implementation**
- Created a RESTful API for Customer with proper CRUD operations
- Added appropriate status codes and validation
Expand Down Expand Up @@ -238,10 +246,10 @@ For this step in the workshop, agent mode should have taken several minutes to c

Agent Mode confirmed that all tests are passing and that the application follows modern ASP.NET best practices with a focus on separation of concerns and test coverage.

3. Finally, verify that the changes and new endpoint is working by starting the BackEnd project from the 'Run and Debug' panel. Once the project is running, test the original URL using your Codespace URL and original endpoint:
3. Finally, verify that the changes and new endpoint are working by starting the BackEnd project from the 'Run and Debug' panel. Once the project is running, test the original URL using your Codespace URL and original endpoint:

```bash
https://< your code space url >.app.github.dev/WeatherForecast
https://< your codespace url >.app.github.dev/WeatherForecast
```

🚀Congratulations! Now you understand the power behind Agent Mode and the many tasks that it can help with. Scroll down to the next section that will show you how to add context and customization to Copilot.
Expand All @@ -255,6 +263,10 @@ Agent Mode was able to perform C# specific tasks:

### 🗒️ Section 4: Customization and Context

🎯 Learning Goals
- Understand the different ways to customize GitHub Copilot and receive better responses from prompts.
- Understand the role of Model Context Protocol (MCP).

**Part 1: Custom Instructions**

GitHub Copilot instructions files are markdown documents that provide essential context to guide Copilot’s behavior within a specific codebase. These files help tailor AI-generated suggestions to match your team’s coding standards, architectural patterns, naming conventions, testing strategies, and deployment practices. There are two types of instructions files: global instructions, which apply to the entire repository and are stored in `copilot-instructions.md`, and scoped instructions, which apply only to specific files or folders and are placed in `.github/instructions/*instructions.md`.
Expand Down
Loading