Skip to content

Instantly share code, notes, and snippets.

@richlander
Last active February 3, 2021 04:36
Show Gist options
  • Save richlander/8b820c44a545b94c8781e74010c627af to your computer and use it in GitHub Desktop.
Save richlander/8b820c44a545b94c8781e74010c627af to your computer and use it in GitHub Desktop.
Various patterns of records
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<Nullable>Enable</Nullable>
</PropertyGroup>
</Project>
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;
}
@kctripathy
Copy link

Nicely explained, thanks a bunch

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