简体   繁体   中英

C# Generics: How to apply a generic type constraint AND inherit from basetype

This is what I am trying to do:

DataService<T> where T : SomeType : DataServiceBase

In other words, I want to introduce a T where it is a subclass of Sometype. But I also need DataService to inherit from DataServiceBase.

It seems I can do one or the other, but not both because there are two colons used. In other words, I can use the generic selector of " where : SomeType" or if I don't use that, I can inherit from DataServiceBase. But if I do both, it generates a design time error.

OK, got it to work like this:

DataService<T> : DataServiceBase where T : SomeType

Greg

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM