1

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?

1 Answer 1

4

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?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.