Skip to content

Instantly share code, notes, and snippets.

@richlander
Last active October 12, 2023 17:02
Show Gist options
  • Save richlander/3b41d7496f2d8533b2d88896bd31e764 to your computer and use it in GitHub Desktop.
Save richlander/3b41d7496f2d8533b2d88896bd31e764 to your computer and use it in GitHub Desktop.
Get Weather Forecast -- with records
using System;
using System.Net.Http;
using System.Net.Http.Json;
string serviceURL = "https://localhost:5001/WeatherForecast";
HttpClient client = new();
Forecast[] forecasts = await client.GetFromJsonAsync<Forecast[]>(serviceURL);
foreach(Forecast forecast in forecasts)
{
Console.WriteLine($"{forecast.Date}; {forecast.TemperatureC}C; {forecast.Summary}");
}
// {"date":"2020-09-06T11:31:01.923395-07:00","temperatureC":-1,"temperatureF":31,"summary":"Scorching"}
public record Forecast(DateTime Date, int TemperatureC, int TemperatureF, string Summary);
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<LangVersion>preview</LangVersion>
</PropertyGroup>
</Project>
@mrsauravsahu
Copy link

All we need now is dotnet run script <path-to-.cs-file> which uses a default .csproj

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment