CSharp Interview Questions

Can one define multiple contraints for single type argument in Generic Class ?

Can one define multiple contraints for single type argument in Generic Class ?

Yes , one can specify multiple contraints for single type argument in Generic class.

Syntax below :
class B<T>  where T:constraint1,contstaint2

B is Generic class
T is Type argument
constraint1 and contraint2 are Generic constraints.

So one can add as many constraints to Type argument seperated by comma.

Example below :

public class Stock<X> where X:IStock,new()
There are two constraints for type argument X :
X must implement IStock
X must have a public default constructor.

Leave a Reply