public T CreateForm<T>() where T: BaseForm, BaseMainForm
I know the above means where T is a BaseForm and a BaseMainForm. But is it also possible to make the constraint that T has to be either a BaseForm or a BaseMainForm?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
No, that is not allowed in C#. The compiler uses the generic constraint to determine what operations is available on
Twithin the generic method – so allowing an or expression would not be type safe.If you need this, consider adding an interface covering the common parts of
BaseFormandBaseMainForm, and apply that as the generic constraint. This way, the interface defines the contract of what the methodCreateForm<T>needs – and you must simply make sure that the Form’s you pass in implement the interface.Something like: