简体   繁体   中英

Generic class definition with both generic type constraint and inherited type?

I was wondering if it possible to have both a generic type constraint and an inherited type?

I could not figure the syntax out for myself or think of a possible example I could look at.

Code Example:

public class Test<T> where T : ITest, new() : IBaseTest {  }

Is it possible to achieve a class definition like this?

public class Test<T>: IBaseTest where T: ITest, new() { }

Your class will implement interface IBaseTest , and generic parameter T constrained to types that are implementing interface ITest and having default (parameterless) constructor.

Is this what you meant?

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