How to inherit from a generic parameter?

后端 未结 4 1999
广开言路
广开言路 2020-12-11 14:23

I\'m basically wanting to do this:

class UILockable : T
    where T : UIWidget
{
}

However, this doesn\'t work. I\'ve seen people

4条回答
  •  清歌不尽
    2020-12-11 15:13

    You can't inherit from a Generic type argument. C# is strictly typed language. All types and inheritance hierarchy must be known at compile time. .Net generics are way different from C++ templates.

    And when you're so sure that type argument T is going to be of type UIWidget then why not inherit from UIWidget itself. Should it ever be allowed [assuming, just assuming, I know this will never be possible] what would you achieve by inheriting your class from T that is already of type UIWidget. At design time, you'll only code against UIWidget, so why not directly inherit from UIWidget.

提交回复
热议问题