DotNext by Roman Sakno

<PackageReference Include="DotNext" Version="0.1.1" />

 Constant<T>

public abstract class Constant<T>
Allows to use constant values as generic parameters.
namespace DotNext.Generic { public abstract class Constant<T> { private static class Cache<G> where G : Constant<T>, new { internal static readonly T Value = (Constant<T>)new G(); } private readonly T Value; protected Constant(T constVal) { Value = constVal; } public sealed override string ToString() { object obj = Value; if (obj != null) return obj.ToString(); return "NULL"; } public sealed override int GetHashCode() { return ((object)Value)?.GetHashCode() ?? 0; } public sealed override bool Equals(object other) { if (other != null) { object obj; if ((obj = other) is T) { T val = (T)obj; return object.Equals(val, Value); } Constant<T> constant; if ((constant = (other as Constant<T>)) != null) { Constant<T> constant2 = constant; return object.Equals(Value, constant2.Value); } } return false; } public static implicit operator T(Constant<T> const) { return const.Value; } public static T Of<G>(bool intern = true) where G : Constant<T>, new { if (!intern) return (Constant<T>)new G(); return Cache<G>.Value; } } }