Last active
October 21, 2021 15:52
-
-
Save richlander/4408b08a03085a345d42ca1d6b753275 to your computer and use it in GitHub Desktop.
Record struct example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Produces the following output:
Record class variant: https://gist.github.com/richlander/bcbbcc9e0b541a06eb805d663ebf6334