50

I want to check if an object is not of a particular type. I know how to check if something is of a particular type:

if (t is TypeA)
{
   ...
}

but

if (t isnt TypeA)
{
   ...
}   

doesn't work.

Andrew
  • 7,602
  • 2
  • 34
  • 42
Sachin Kainth
  • 45,256
  • 81
  • 201
  • 304
  • possible duplicate of [Check if object is NOT of type (!= equivalent for "IS") - C#](http://stackoverflow.com/questions/529944/check-if-object-is-not-of-type-equivalent-for-is-c-sharp) – nobody Jul 03 '14 at 16:34

7 Answers7

91

UPDATE 2020-10-30:

Times are changing. Starting from C# 9.0 you can use more natural way of checking it:

if(t is not TypeA) { ... }

ORIGINAL ANSWER:

C# is not quite natural language ;) Use this one

if(!(t is TypeA))
{
   ...
}
ie.
  • 5,982
  • 1
  • 29
  • 44
10

if you want not only check, you can use as operator.

var a = t as TypeA;
if(a!= null)
   //use a.. 

In this way, if you want use a type after check, you avoid double casting..

Tigran
  • 61,654
  • 8
  • 86
  • 123
5

Extensions methods to the rescue!!

public static class ObjectExtensions
{
    public static bool Isnt(this object source, Type targetType)
    {
        return source.GetType() != targetType;
    }
}

Usage

if (t.Isnt(typeof(TypeA)))
{
   ...
}
Paleta
  • 970
  • 2
  • 13
  • 27
  • I did this, but I also created an extension of "IS" as well just so the code looks similar -e.g. t.IsA(xxx) , t.IsNotA(xxx). yes I know it's not needed, but it looks better to me. – Harag Sep 30 '15 at 13:55
4

If you are doing a TypeA x = (TypeA)t; inside the if block then a better way is

TypeA x = t as TypeA
if(x != null)
{
...
}

This causes only one time type checking rather than twice.

softveda
  • 10,858
  • 6
  • 42
  • 50
4

Short answer: you may want to use:

if (t.GetType()==typeof(TypeA))
{
   ...
}
if (t.GetType()!=typeof(TypeA))
{
  ...
}

Long answer:

So. Be aware that you're asking if it's a particular type. is doesn't tell you if it's a particular type - it tells you if it's a particular type or any descendant of that type.

So if you have two classes, Animal, and Cat : Animal, and felix is a cat, then

if (felix is Animal)
{
    //returns true
}
if (felix.GetType() == typeof(Animal))
{
    //does not
}

If it's not important to you, inherited classes are okay, then don't worry about it, use !(felix is Animal) as others mentioned, and you're fine! (You're probably fine.)

But if you need to be sure felix is specifically an Animal, then you need to ask if t.GetType()==typeof(TypeA).

Peter Kay
  • 133
  • 7
2

I usually stick the null and type checking all in one line:

if (t == null || !(t is TypeA)) {
  ...
}

If TypeA is a struct, you'll need to handle it slightly differently again:

if (t == null || t.GetType() != typeof(TypeA)) {
  ...
}
Ross
  • 4,460
  • 2
  • 32
  • 59
0

Check below example for getType():

object obj = new object();
obj.GetType();

string s;
s.GetType();

List<string> StringList = new List<string>();
StringList.GetType();
Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
  • 2
    Link only answers are not that efficient here. Please edit your answer with relevant code so that it's easier for everyone to read what's important to the subject. – dachi Feb 27 '14 at 08:43