<p>I know how to test an object to see if it is of a type, using the IS keyword e.g.</p>

<pre><code>if (foo is bar)
{
  //do something here
}
</code></pre>

<p>but how do you test for it not being "bar"?, I can't seem to find a keyword that works with IS to test for a negative result. </p>

<p>BTW  - I have a horrible feeling this is soooo obvious, so apologies in advance...</p>

[{'answer_id': 55980, 'author': 'Vinko Vrsalovic', 'author_id': 5190, 'author_profile': 'https://Stackoverflow.com/users/5190', 'pm_score': 5, 'selected': True, 'text': '<pre><code>if (!(foo is bar)) {\n}\n</code></pre>\n'}, {'answer_id': 55981, 'author': 'aku', 'author_id': 1196, 'author_profile': 'https://Stackoverflow.com/users/1196', 'pm_score': 1, 'selected': False, 'text': '<p>There is no specific keyword</p>\n\n<pre><code>if (!(foo is bar)) ...\nif (foo.GetType() != bar.GetType()) .. // foo &amp; bar should be on the same level of type hierarchy\n</code></pre>\n'}, {'answer_id': 55991, 'author': 'jfs', 'author_id': 718, 'author_profile': 'https://Stackoverflow.com/users/718', 'pm_score': 2, 'selected': False, 'text': '<p>You can also use the <a href="http://msdn.microsoft.com/en-us/library/cscsdfbt(VS.71).aspx" rel="nofollow noreferrer">as operator</a>.</p>\n\n<blockquote>\n  <p>The as operator is used to perform\n  conversions between compatible types.</p>\n</blockquote>\n\n<pre><code>bar aBar = foo as bar; // aBar is null if foo is not bar\n</code></pre>\n'}, {'answer_id': 85100, 'author': 'Haacked', 'author_id': 598, 'author_profile': 'https://Stackoverflow.com/users/598', 'pm_score': 1, 'selected': False, 'text': '<p>You should clarify whether you want to test that an object is <em>exactly</em> a certain type or <em>assignable</em> from a certain type. For example:</p>\n\n<pre><code>public class Foo : Bar {}\n</code></pre>\n\n<p>And suppose you have: </p>\n\n<pre><code>Foo foo = new Foo();\n</code></pre>\n\n<p>If you want to know whether foo is not Bar(), then you would do this:</p>\n\n<pre><code>if(!(foo.GetType() == tyepof(Bar))) {...}\n</code></pre>\n\n<p>But if you want to make sure that foo does not derive from Bar, then an easy check is to use the as keyword.</p>\n\n<pre><code>Bar bar = foo as Bar;\nif(bar == null) {/* foo is not a bar */}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/55978', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/5175/']