开发者

C# Template as base type? if not . how?

开发者 https://www.devze.com 2023-03-14 06:28 出处:网络
class Beta : IBeta class AlphaBeta<TBeta&开发者_运维技巧gt; : TBeta where Tbeta : IBeta Tbeta needs to be a type... compiler errr.
class Beta : IBeta

class AlphaBeta<TBeta&开发者_运维技巧gt; : TBeta
    where Tbeta : IBeta

Tbeta needs to be a type... compiler errr.

how can I get AlphaBeta to be a Beta?

ty


You can't do that - generics in .NET aren't the same as templates in C++.

You can give AlphaBeta a member variable of type TBeta, but you can't make it derive from TBeta.

If you can give more information about what you're trying to do at a "bigger picture" level, we may be able to give more help. Would implementing IBeta be acceptable instead?


I would suggest

    class AlphaBeta<T> : Beta
        where T : IBeta

Because T is the C# generic type handler. You then use constraints to force T to be a class that inherits the IBeta class.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号