17
if(myVariable is SomeType)

Out of nothing but curiosity, what's the opposite of that? I want to do something like:

if(!myVariable is SomeType)
if(myVariable is not SomeType)

Neither compile.

Given that "is" is a non-searchable word in most engines, this has been a hard one to find an answer to.

Duplicate:

C# : ‘is’ keyword and checking for Not

1
  • Usually you can still search for "is" in a search engine if you do +"is" in your search query by requiring to match on is.
    – Alex
    Aug 27, 2009 at 22:50

7 Answers 7

45

Try

if (!(myVariable is SomeType))
1
  • The ! should be inside the bracket. if(!(myVariable is SomeType)
    – Brandon
    Aug 27, 2009 at 22:49
8

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.

6
  • 1
    +1 for the polymorphism comment. Maybe Deane doesn't need it, but it's certainly nice to have it mentioned near code like this. Aug 27, 2009 at 22:52
  • It is pretty common to check to see if an object is of a certain type. The foreach construct does it to see if the enumerator implements IDisposable. Aug 28, 2009 at 1:06
  • it should not be pretty common in your code though. There are not many tasks in C# that would require the use of checking the type explicitly instead of using polymorphism. I'm not saying that it is not valid in some situations, but it should not be "common". Aug 28, 2009 at 7:37
  • Sorry, I'm going to have to disagree. For example, polymorphism will not help you override the Equals(object) method. But, 'is' or 'as' certainly will. Polymorphism definitely helps if you are wanting get rid of those pesky switch or if statements though. Aug 28, 2009 at 14:27
  • What's the first thing you do inside an overriden Equals method? I check to see if the argument is of the same type as the class containing the Equals method. I do that with the 'is' or 'as' operators and bail out early if fails the test. The same goes for the IComparable.CompareTo method and many others. I use the 'is' and 'as' operators a lot and those examples only scratch the surface. It's a great way to avoid an exception when you're not sure a traditional cast will succeed. Aug 29, 2009 at 0:51
4

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.

3

Note that C# 9.0 now supports is not.

For example:

if (e is not null)
{
    // ...
}

https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-9#pattern-matching-enhancements

1
  • 1
    I did note this. A nice improvement.
    – Deane
    Dec 8, 2020 at 13:19
2

I'm not in front of a compiler right now so can't check, but wouldn't

if (!(myVariable is SomeType))

work?

1
if(!(myVariable is SomeType))

From C# : 'is' keyword and checking for Not

0
0

Or how about a generic extension? I prefer this way to avoid having so many parenthesis.

So you have:

if (!MyObject.Is<string>())
    //Do blah blah blah

Using:

public static class ObjectExtensions
{
    public static bool Is<T>(this object ToEvaluate) 
    {
        return ToEvaluate is T;
    }
}

Which seems easier to read too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.