From 254b3cbf71da393daa16d87aa661e2dd455dc77a Mon Sep 17 00:00:00 2001 From: "Vic Perdana (MSFT)" <7114832+vicperdana@users.noreply.github.com> Date: Fri, 23 Jan 2026 16:09:41 +1100 Subject: [PATCH 1/2] docs: add .NET example for weather assistant using Copilot SDK --- docs/getting-started.md | 68 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/docs/getting-started.md b/docs/getting-started.md index 7833d074..aad966e5 100644 --- a/docs/getting-started.md +++ b/docs/getting-started.md @@ -779,6 +779,74 @@ python weather_assistant.py +
+.NET + +Create a new console project and update `Program.cs`: + +```csharp +using GitHub.Copilot.SDK; +using Microsoft.Extensions.AI; +using System.ComponentModel; + +// Define the weather tool using AIFunctionFactory +var getWeather = AIFunctionFactory.Create( + ([Description("The city name")] string city) => + { + var conditions = new[] { "sunny", "cloudy", "rainy", "partly cloudy" }; + var random = new Random(); + var temp = random.Next(50, 80); + var condition = conditions[random.Next(conditions.Length)]; + return new { city, temperature = $"{temp}°F", condition }; + }, + "get_weather", + "Get the current weather for a city"); + +await using var client = new CopilotClient(); +await using var session = await client.CreateSessionAsync(new SessionConfig +{ + Model = "gpt-4.1", + Streaming = true, + Tools = [getWeather] +}); + +// Listen for response chunks +session.On(ev => +{ + if (ev is AssistantMessageDeltaEvent deltaEvent) + { + Console.Write(deltaEvent.Data.DeltaContent); + } +}); + +Console.WriteLine("🌤️ Weather Assistant (type 'exit' to quit)"); +Console.WriteLine(" Try: 'What's the weather in Paris?' or 'Compare weather in NYC and LA'\n"); + +while (true) +{ + Console.Write("You: "); + var input = Console.ReadLine(); + + if (string.IsNullOrEmpty(input) || input.Equals("exit", StringComparison.OrdinalIgnoreCase)) + { + break; + } + + Console.Write("Assistant: "); + await session.SendAndWaitAsync(new MessageOptions { Prompt = input }); + Console.WriteLine("\n"); +} +``` + +Run with: + +```bash +dotnet run +``` + +
+ + **Example session:** ``` From f2dd7c7169fb25b83c698ff3e7e2280a25b684d2 Mon Sep 17 00:00:00 2001 From: "Vic Perdana (MSFT)" <7114832+vicperdana@users.noreply.github.com> Date: Fri, 23 Jan 2026 21:48:40 +1100 Subject: [PATCH 2/2] fix: address code review feedback for .NET example - Use Random.Shared instead of new Random() for thread-safe random generation - Add SessionIdleEvent handler for consistent output formatting --- docs/getting-started.md | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/docs/getting-started.md b/docs/getting-started.md index aad966e5..69cad254 100644 --- a/docs/getting-started.md +++ b/docs/getting-started.md @@ -794,9 +794,8 @@ var getWeather = AIFunctionFactory.Create( ([Description("The city name")] string city) => { var conditions = new[] { "sunny", "cloudy", "rainy", "partly cloudy" }; - var random = new Random(); - var temp = random.Next(50, 80); - var condition = conditions[random.Next(conditions.Length)]; + var temp = Random.Shared.Next(50, 80); + var condition = conditions[Random.Shared.Next(conditions.Length)]; return new { city, temperature = $"{temp}°F", condition }; }, "get_weather", @@ -817,6 +816,10 @@ session.On(ev => { Console.Write(deltaEvent.Data.DeltaContent); } + if (ev is SessionIdleEvent) + { + Console.WriteLine(); + } }); Console.WriteLine("🌤️ Weather Assistant (type 'exit' to quit)");