Multiple Generic Constraints .Net
In this article, we will learn how to apply constraints with generics.
Generic Constraints In Action
Getting Started
In above link, we have learned about generics. Consider below example of simple generic type in which compiler does not know about placeholder type whether its a class or a interface etc.
To overcome that, we apply constraints to generics using “where” keyword. Constraints will result in compile time error if you instantiate with placeholder that is not defined in constraints.
For example, here we created a generic with two placeholders named “T1” and “T2” with constraints that T1 can only accept classes of “ModelExample3” and T2 with “ModelExample2”.