Total .NET Analyzer Rule Documentation  

RedundantCast Rule

Remove redundant explicit type casting.

Remarks

Avoid casting an object to a type that it already is (i.e. you are casting an object of type x to type x). For example, the following type cast is redundant:


' VB
Dim x as Integer = 0
Dim y as Integer = 0
Dim z As Integer = Ctype(x + y, Integer)

// C#
int x = 0;
int y = 1;
int z = (int)x + y;


Although this does not affect the result of the calculation, it can cause a slight performance degradation. Above all, however, the explicit type cast is unnecessary and can make your code difficult to read and maintain.

Resolution

Remove unnecessary explicit type casts. For instance, you could modify the code above to:

' VB
Dim x as Integer = 0
Dim y as Integer = 0
Dim z As Integer = x + y

// C#
int x = 0;
int y = 1;
int z = x + y

See Also