Skip to content

Instantly share code, notes, and snippets.

@richlander
Last active October 13, 2020 14:31
Show Gist options
  • Save richlander/17d7455573b14ef88a6b957e1e21c271 to your computer and use it in GitHub Desktop.
Save richlander/17d7455573b14ef88a6b957e1e21c271 to your computer and use it in GitHub Desktop.
Record with static helper
using System;
var weight = 200;
WeightMeasurement measurement = new(DateTime.Now, weight)
{
Pounds = WeightMeasurement.GetPounds(weight)
};
Console.WriteLine(measurement);
public record WeightMeasurement(DateTime Date, double Kilograms)
{
public double Pounds {get; init;}
public static double GetPounds(double kilograms) => kilograms * 2.20462262;
}
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<Nullable>Enable</Nullable>
</PropertyGroup>
</Project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment