Created
April 25, 2023 20:58
-
-
Save daiplusplus/52b9617919fb02e2fa9e4c39aad7fb03 to your computer and use it in GitHub Desktop.
This is why I dislike init properties in C#
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
using System; | |
using System.Globalization; | |
public static class Program | |
{ | |
public static void Main() | |
{ | |
AReddishColor pink = new AReddishColor( r: 244, g: 189, b: 224 ); | |
Console.WriteLine( "pink: {0}", pink ); | |
AReddishColor pinkAtFirstButThenSubverted = new AReddishColor( r: 244, g: 189, b: 224 ) | |
{ | |
R = 0 | |
}; | |
Console.WriteLine( "not pink: {0}", pinkAtFirstButThenSubverted ); | |
} | |
} | |
/** Represents an RGB color such that the R value is always greater than G or B */ | |
public sealed class AReddishColor | |
{ | |
public AReddishColor( Byte r, Byte g, Byte b ) | |
{ | |
if( r <= g ) throw new ArgumentOutOfRangeException( message: "Red value must be greater than the Green and Blue values.", actualValue: r, paramName: nameof(r) ); | |
if( r <= b ) throw new ArgumentOutOfRangeException( message: "Red value must be greater than the Green and Blue values.", actualValue: r, paramName: nameof(r) ); | |
this.R = r; | |
this.G = g; | |
this.B = b; | |
} | |
public Byte R { get; init; } | |
public Byte G { get; init; } | |
public Byte B { get; init; } | |
public override string ToString() | |
{ | |
return String.Format( CultureInfo.InvariantCulture, format: "#{0:X2}{1:X2}{2:X2}", this.R, this.G, this.B ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment