Created
October 11, 2021 21:56
-
-
Save richlander/034ed68fcf9387b716e698ac6f414c22 to your computer and use it in GitHub Desktop.
readonly record struct example
This file contains hidden or 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 hidden or 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 updatedBattery = battery with {RemainingCapacityPercentage = battery.RemainingCapacityPercentage - 1}; | |
battery = updatedBattery; | |
} | |
WriteLine(battery); | |
public readonly 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: