Synopsis: | Use generic constraints if applicable |
Language: | C# |
Severity Level: | 5 |
Category: | Object oriented |
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; } } Correct example: class MyClass<T> where T : SomeClass { void SomeMethod(T t) { SomeClass obj = t; } } |
Literature References: |
Aviva AV1240 |