-
-
Save richlander/8b820c44a545b94c8781e74010c627af to your computer and use it in GitHub Desktop.
Various patterns of records
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> | |
<Nullable>Enable</Nullable> | |
</PropertyGroup> | |
</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
using System; | |
using static System.Console; | |
var one = new One("a", 1); | |
var oneprime = one with {Prop1 = "b", Prop2 = 2}; | |
WriteLine(one); | |
WriteLine(oneprime); | |
WriteLine($"Equality: {one == oneprime}"); | |
var two = new Two("a") {Prop2 = 1}; | |
var twoprime = two with {Prop1 = "b", Prop2 = 2}; | |
WriteLine(two); | |
WriteLine(twoprime); | |
var three = new Three("a", 1); | |
var threeprime = three with {Prop1 = "b"}; | |
WriteLine(three); | |
WriteLine(threeprime); | |
var four = new Four("a", 1); | |
WriteLine(four); | |
public record One(string Prop1, int Prop2); | |
public record OneEquivalent | |
{ | |
public OneEquivalent(string prop1, int prop2) | |
{ | |
Prop1 = prop1; | |
Prop2 = prop2; | |
} | |
public string Prop1 { get; init; } | |
public int Prop2 { get; init; } | |
} | |
public record Two(string Prop1) | |
{ | |
public int Prop2 { get; init; } | |
} | |
public record Three | |
{ | |
public Three(string prop1, int prop2) | |
{ | |
Prop1 = prop1; | |
Prop2 = prop2; | |
} | |
public string Prop1 { get; init; } | |
public int Prop2 { get; } | |
} | |
public record Four(string prop1, int prop2) | |
{ | |
public string Prop1 => prop1; | |
public int Prop2 => prop2; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nicely explained, thanks a bunch