ReSharper 2017.3 Help

Code Inspection: Redundant cast

According to C# documentation, explicit conversion (cast) is redundant in the following cases:

  • Conversions from derived classes to base classes.
  • Conversions from smaller to larger integral types.

In these cases the cast is done implicitly by compiler. Needless to say, duplicate casts and casts between the same type are also redundant.

ReSharper suggests removing the explicit cast if no information will be lost in the implicit conversion or if the conversion will always succeed.

Suboptimal codeAfter the quick-fix
class Base { } class Derived : Base { } class Program { static void Main() { Derived d = new Derived(); Base b = (Base) d; // ... } }
class Base { } class Derived : Base { } class Program { static void Main() { Derived d = new Derived(); Base b = d; // ... } }
Last modified: 7 December 2017

See Also