I am porting a Java library in C#, and I found this weird class that uses a generics circular reference:
public static class UserAgentAnalyzerDirectBuilder<UAA extends UserAgentAnalyzerDirect, B extends UserAgentAnalyzerDirectBuilder<UAA, B>>
{
// Implementation
}
I can't well understand how a circular reference can work, but as I see is indeed possible.
That code can be easily translated to C#:
public class UserAgentAnalyzerDirectBuilder<UAA, B> where UAA: UserAgentAnalyzerDirect where B: UserAgentAnalyzerDirectBuilder<UAA, B>
{
// Implementation
}
Assuming my equivalent class is correct, I need to implement the method that instantiate that weird class.
In Java I have this code:
public static UserAgentAnalyzerDirectBuilder<? extends UserAgentAnalyzer, ? extends UserAgentAnalyzerDirectBuilder<?, ?>> newBuilder() {
return new UserAgentAnalyzerDirectBuilder<>(new UserAgentAnalyzer());
}
In C# I tried to reproduce with:
public static UserAgentAnalyzerDirectBuilder<UAA, UserAgentAnalyzerDirectBuilder<UAA, B>> newBuilder<UAA, B>()
where UAA: UserAgentAnalyzerDirect
where B: UserAgentAnalyzerDirectBuilder<UAA, B>
{
return new UserAgentAnalyzerDirectBuilder<UAA, UserAgentAnalyzerDirectBuilder<UAA, B>> (new UserAgentAnalyzer());
}
But it doesn't work. I am wondering if I did something wrong or if in C# generics circular references are indeed possible.