Synopsis: | Use generic constraints if applicable |
Description: |
Instead of casting to and from the object type in generic types or methods, use where constraints or the as operator to specify the exact characteristics of the generic parameter. Wrong example: class MyClass<T> { void SomeMethod(T t) { object temp = t; SomeClass obj = (SomeClass) temp; } } Wrong example: class MyClass<T> where T : SomeClass { void SomeMethod(T t) { SomeClass obj = t; } } |
Literature References: |
Aviva AV1240 |