What's the opposite of "is"?

Try

if (!(myVariable is SomeType))

You need to surround the statement in parentheses.

if ( !myVariable is SomeType )

That line applies the NOT operator to myVariable, not the entire statement. Try:

if ( !( myVariable is SomeType ) )

Although, I would be wary of code that checks an object for its type anyhow. You may want to look into the concept of polymorphism.


Jay and marc have the jist of it. Alternatively, you could do:

var cast = myVariable as SomeType;
if(cast == null)
{
  // myVariable is not SomeType
}

The benefit of this method is that you now have a variable already cast as SomeType immediately available for use.

Tags:

C#