C# : 'is' keyword and checking for Not


C# Problem Overview

This is a silly question, but you can use this code to check if something is a particular type...

if (child is IContainer) { //....

Is there a more elegant way to check for the "NOT" instance?

if (!(child is IContainer)) { //A little ugly... silly, yes I know...

//these don't work :)
if (child !is IContainer) {
if (child isnt IContainer) { 
if (child aint IContainer) { 
if (child isnotafreaking IContainer) { 

Yes, yes... silly question....

Because there is some question on what the code looks like, it's just a simple return at the start of a method.

public void Update(DocumentPart part) {
    if (!(DocumentPart is IContainer)) { return; }
    foreach(DocumentPart child in ((IContainer)part).Children) {

C# Solutions

Solution 1 - C#

if(!(child is IContainer))

is the only operator to go (there's no IsNot operator).

You can build an extension method that does it:

public static bool IsA<T>(this object obj) {
    return obj is T;

and then use it to:

if (!child.IsA<IContainer>())

And you could follow on your theme:

public static bool IsNotAFreaking<T>(this object obj) {
    return !(obj is T);

if (child.IsNotAFreaking<IContainer>()) { // ...

Update (considering the OP's code snippet):

Since you're actually casting the value afterward, you could just use as instead:

public void Update(DocumentPart part) {
    IContainer containerPart = part as IContainer;
    if(containerPart == null) return;
    foreach(DocumentPart child in containerPart.Children) { // omit the cast.

Solution 2 - C#

You can do it this way:

object a = new StreamWriter("c:\\temp\\test.txt");

if (a is TextReader == false)

Solution 3 - C#

New In C# 9.0


if (part is not IContainer)

Original Answer

This hasn't been mentioned yet. It works and I think it looks better than using !(child is IContainer)

if (part is IContainer is false)

Solution 4 - C#

C# 9 (released with .NET 5) includes the logical patterns and, or and not, which allows us to write this more elegantly:

if (child is not IContainer) { ... }

Likewise, this pattern can be used to check for null:

if (child is not null) { ... }

Solution 5 - C#

Why not just use the else ?

if (child is IContainer)
  // Do what you want here

Its neat it familiar and simple ?

Solution 6 - C#

The way you have it is fine but you could create a set of extension methods to make "a more elegant way to check for the 'NOT' instance."

public static bool Is<T>(this object myObject)
    return (myObject is T);

public static bool IsNot<T>(this object myObject)
    return !(myObject is T);

Then you could write:

if (child.IsNot<IContainer>())
    // child is not an IContainer

Solution 7 - C#

Ugly? I disagree. The only other way (I personally think this is "uglier"):

var obj = child as IContainer;
if(obj == null)
   //child "aint" IContainer

Solution 8 - C#

The is operator evaluates to a boolean result, so you can do anything you would otherwise be able to do on a bool. To negate it use the ! operator. Why would you want to have a different operator just for this?

Solution 9 - C#

The extension method IsNot<T> is a nice way to extend the syntax. Keep in mind

var container = child as IContainer;
if(container != null)
  // do something w/ contianer

performs better than doing something like

if(child is IContainer)
  var container = child as IContainer;
  // do something w/ container

In your case, it doesn't matter as you are returning from the method. In other words, be careful to not do both the check for type and then the type conversion immediately after.

Solution 10 - C#

While this doesn't avoid the problem of parentheses, for the sake of people getting here via Google, it should be mentioned that newer syntax exists (as of C# 7) to make the rest of your code a little cleaner:

if (!(DocumentPart is IContainer container)) { return; }
foreach(DocumentPart child in container.Children) {

This avoids the double-cast, the null-check, and having a variable available in scopes where it could be null.

Solution 11 - C#

While the IS operator is normally the best way, there is an alternative that you can use in some cirumstances. You can use the as operator and test for null.

MyClass mc = foo as MyClass;
if ( mc == null ) { }
else {}

Solution 12 - C#

Ill use this

If(!(object is Car)){


Solution 13 - C#

if (child is IContainer ? false : true)


All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionhugowareView Question on Stackoverflow
Solution 1 - C#mmxView Answer on Stackoverflow
Solution 2 - C#cjkView Answer on Stackoverflow
Solution 3 - C#Todd SkeltonView Answer on Stackoverflow
Solution 4 - C#Thorkil Holm-JacobsenView Answer on Stackoverflow
Solution 5 - C#Mark BroadhurstView Answer on Stackoverflow
Solution 6 - C#Robert CartainoView Answer on Stackoverflow
Solution 7 - C#BFreeView Answer on Stackoverflow
Solution 8 - C#Brian RasmussenView Answer on Stackoverflow
Solution 9 - C#JeffView Answer on Stackoverflow
Solution 10 - C#StriplingWarriorView Answer on Stackoverflow
Solution 11 - C#Muad'DibView Answer on Stackoverflow
Solution 12 - C#Daniel SørensenView Answer on Stackoverflow
Solution 13 - C#TernaryView Answer on Stackoverflow