What is the opposite of "eat"?

if(myVariable is SomeType) 

Out of nothing but curiosity, what is the opposite of this? I want to do something like:

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

Neither compile.

Given that the word "is" is a non-search word in most engines, it was difficult to find the answer.

Duplicate:

C #: 'this is a keyword and check for Not

+6
c #
source share
6 answers

Try

 if (!(myVariable is SomeType)) 
+28
source share

You need to surround the statement in parentheses.

 if ( !myVariable is SomeType ) 

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

 if ( !( myVariable is SomeType ) ) 

Although, I will be wary of code that still checks the object for its type. You can take a look at the concept of polymorphism.

+7
source share

Jay and Mark have a vault. Alternatively, you can:

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

The advantage of this method is that now you already have a variable that is already used as SomeType, which is immediately available for use.

+4
source share

I am not in front of the compiler now, so I can’t check, but not

 if (!(myVariable is SomeType)) 

Job?

+2
source share
 if(!(myVariable is SomeType)) 

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

+1
source share

Or what about general expansion? I prefer this method to avoid a lot of parentheses.

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 is also easier to read.

0
source share

All Articles