-
-
Save richlander/83d2ccde88218e56e6b93a55db3f986b to your computer and use it in GitHub Desktop.
Records with mutable data
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
using System; | |
Battery battery = new Battery("CR2032", 0.235) | |
{ | |
RemainingCapacityPercentage = 100 | |
}; | |
Console.WriteLine (battery); | |
for (int i = battery.RemainingCapacityPercentage; i >= 0; i--) | |
{ | |
battery.RemainingCapacityPercentage = i; | |
} | |
Console.WriteLine (battery); | |
public record Battery(string Model, double TotalCapacityAmpHours) | |
{ | |
public int RemainingCapacityPercentage {get;set;} | |
} |
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>net5.0</TargetFramework> | |
<LangVersion>preview</LangVersion> | |
<Nullable>enable</Nullable> | |
</PropertyGroup> | |
</Project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Produces the following output:
Alternate version, using only immutable data: https://gist.github.com/richlander/bcbbcc9e0b541a06eb805d663ebf6334.