Search code examples
c#genericstypesnested-generics

Classes coupled with each other by generics


I'm currently learning C# and I'm coming from Java. In Java, I can do something like this:

public interface BasePresenterView<T extends BaseActivityPresenter> {}
public interface BaseActivityPresenter<T extends BasePresenterView> {}

and in C# I'm having a hard time achieving the same thing.


Solution

  • I think I resolved it :)

    public interface BaseActivityPresenter<T, K> 
            where T : BasePresenterView<T, K>
            where K : BaseActivityPresenter<T, K> {}
    
    public interface BasePresenterView<T, K> 
            where T : BasePresenterView<T, K>
            where K : BaseActivityPresenter<T, K> {}
    

    That seems to be working... for now. I don't know if this is a proper approach.