Skip to content

Instantly share code, notes, and snippets.

@richlander
Last active October 21, 2021 15:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save richlander/4408b08a03085a345d42ca1d6b753275 to your computer and use it in GitHub Desktop.
Save richlander/4408b08a03085a345d42ca1d6b753275 to your computer and use it in GitHub Desktop.
Record struct example
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<Using Include="System.Console" Static="True"/>
</ItemGroup>
</Project>
Battery battery = new("CR2032", 0.235, 100);
WriteLine(battery);
while (battery.RemainingCapacityPercentage > 0)
{
battery.RemainingCapacityPercentage--;
}
WriteLine(battery);
public record struct Battery(string Model, double TotalCapacityAmpHours, int RemainingCapacityPercentage);
@richlander
Copy link
Author

richlander commented Oct 10, 2021

Produces the following output:

Battery { Model = CR2032, TotalCapacityAmpHours = 0.235, RemainingCapacityPercentage = 100 }
Battery { Model = CR2032, TotalCapacityAmpHours = 0.235, RemainingCapacityPercentage = 0 }

Record class variant: https://gist.github.com/richlander/bcbbcc9e0b541a06eb805d663ebf6334

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