Reputation: 331
void LoadParameters<T, TValue>(TValue strategy)
where T : Parameters
Saw a code like this. Will the program still work correctly without the where statement?
Upvotes: 0
Views: 92
Reputation: 54427
That where
clause is called a "generic type constraint". Usually T
can be any type and you must write your generic code with that in mind. By constraining T
to be, inherit or implement a specific type, you gain the ability to refer to the members of that type in your generic code, because the compiler is assured that any object used will be that type. You can also use class
, struct
or new
as generic type constraints, which enforce T
being a reference type, a value type or having a parameterless constructor respectively.
Upvotes: 1
Reputation: 77304
If it compiles without the where statement, it will work correctly. However, chances are the coder did not simply put it there without reason. If you remove it, it will likely not compile and hence not work.
Upvotes: 4