C# generic ClientBase with interface confusion

前端 未结 1 1863
旧巷少年郎
旧巷少年郎 2021-01-18 20:40

The C# definition of ClientBase is:

public abstract class ClientBase : ICommunicationObject, 
    IDisposable
where TChannel : class
<         


        
相关标签:
1条回答
  • 2021-01-18 21:07

    The class constraint constraints the generic type to a reference type. An interface is, by definition, a reference type. What you can't do is use a value type as the generic type: ClientBase<int> would be a compile time error.

    As to the second error, you are not constraining TInterface and then using it inClientBase<TInterface>. Because ClientBase constraints its generic type to reference types (class), you need to constraint TInterface accordingly.

    0 讨论(0)
提交回复
热议问题