interface ISomeFactory<out TSelf> {
static abstract TSelf CreateInstance(int a, string b, bool c);
}
class SomeClass : ISomeFactory<SomeClass> {
public int A { get; init; }
public string B { get; init; }
public bool C { get; init; }
public static SomeClass CreateInstance(int a, string b, bool c) {
return new SomeClass { A = a, B = b, C = c };
}
}
static T InstantiateItemGenerically<T>(int a, string b, bool c) where T : ISomeFactory<T> {
return T.CreateInstance(a, b, c);
}
static void Test() {
var someClass = InstantiateItemGenerically<SomeClass>(1, "hello", true);
Console.WriteLine($"{someClass.A},{someClass.B},{someClass.C}"); // Prints "1,hello,True" on console
}
Code snippet taken from "Complete C# Quick Reference - C# 11".