Skip to content

Instantly share code, notes, and snippets.

@Aetopia
Created June 5, 2026 13:38
Show Gist options
  • Select an option

  • Save Aetopia/62be7835f7441e2420e2c66bfb25a3a5 to your computer and use it in GitHub Desktop.

Select an option

Save Aetopia/62be7835f7441e2420e2c66bfb25a3a5 to your computer and use it in GitHub Desktop.
"Static" Abstract Classes
using System;
static class Program
{
static void Main()
{
A.Method();
new B().Method();
A.Method();
new B().Method();
}
}
sealed class A : Template.Static<A.Instance>
{
internal sealed class Instance : Template.Instance<Instance>
{
internal override string String => nameof(A);
}
}
sealed class B : Template
{
internal override string String => nameof(B);
}
abstract class Template
{
internal abstract string String { get; }
internal void Method() => Console.WriteLine(String);
internal abstract class Instance<T> : Template where T : Instance<T>, new()
{
public Instance() => Console.WriteLine(typeof(Instance<T>));
internal static readonly T s_this = new();
}
internal abstract class Static<T> where T : Instance<T>, new()
{
public Static() => Console.WriteLine(typeof(Static<T>));
static readonly T s_this = Instance<T>.s_this;
internal static string String => s_this.String;
internal static void Method() => s_this.Method();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment