Skip to content

Const members of generic types

Blokyk edited this page Jul 5, 2021 · 1 revision

It should be possible to access constant of a constrained generic type (obviously, only if the base constrain class has any const/static members)

Example :

// @INode.cs
public interface INode<this TThis> {
   public const TThis NULL;
}
// @Parser.cs
public class Parser<T> where T is INode {
   public static readonly T Default => T.NULL;
}
// @SomeFile.cs
public void GetNullOf<T>(T t) => t.NULL;