Our website uses cookies to enhance your browsing experience.
Accept
to the top
>
>
>
V3131. The expression is checked for...
menu mobile close menu
Additional information
toggle menu Contents

V3131. The expression is checked for compatibility with the type 'A', but is casted to the 'B' type.

Jan 30 2017

The analyzer detected a likely error that has to do with checking if an expression is compatible with one type and casting it to another type inside the body of the conditional statement.

Consider the following example:

if (obj is A)
{
  return (B)obj;
}

The programmer must have made a mistake, since a type conversion like that is very likely to cause a bug. What was actually meant is either to check the expression for type 'B' or cast it to type 'A'.

This is what the correct version could look like:

if (obj is B)
{
  return (B)obj;
}

This diagnostic is classified as:

You can look at examples of errors detected by the V3131 diagnostic.