HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpCritical

Type Checking: typeof, GetType, or is?

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
gettypetypetypeofchecking

Problem

I've seen many people use the following code:

Type t = typeof(SomeType);
if (t == typeof(int))
    // Some code here


But I know you could also do this:

if (obj1.GetType() == typeof(int))
    // Some code here


Or this:

if (obj1 is int)
    // Some code here


Personally, I feel the last one is the cleanest, but is there something I'm missing? Which one is the best to use, or is it personal preference?

Solution

All are different.

  • typeof takes a type name (which you specify at compile time).



  • GetType gets the runtime type of an instance.



  • is returns true if an instance is in the inheritance tree.



Example

class Animal { } 
class Dog : Animal { }

void PrintTypes(Animal a) { 
    Console.WriteLine(a.GetType() == typeof(Animal)); // false 
    Console.WriteLine(a is Animal);                   // true 
    Console.WriteLine(a.GetType() == typeof(Dog));    // true
    Console.WriteLine(a is Dog);                      // true 
}

Dog spot = new Dog(); 
PrintTypes(spot);



What about typeof(T)? Is it also resolved at compile time?

Yes. T is always what the type of the expression is. Remember, a generic method is basically a whole bunch of methods with the appropriate type. Example:

string Foo(T parameter) { return typeof(T).Name; }

Animal probably_a_dog = new Dog();
Dog    definitely_a_dog = new Dog();

Foo(probably_a_dog); // this calls Foo and returns "Animal"
Foo(probably_a_dog); // this is exactly the same as above
Foo(probably_a_dog); // !!! This will not compile. The parameter expects a Dog, you cannot pass in an Animal.

Foo(definitely_a_dog); // this calls Foo and returns "Dog"
Foo(definitely_a_dog); // this is exactly the same as above.
Foo(definitely_a_dog); // this calls Foo and returns "Animal". 
Foo((Animal)definitely_a_dog); // this does the same as above, returns "Animal"

Code Snippets

class Animal { } 
class Dog : Animal { }

void PrintTypes(Animal a) { 
    Console.WriteLine(a.GetType() == typeof(Animal)); // false 
    Console.WriteLine(a is Animal);                   // true 
    Console.WriteLine(a.GetType() == typeof(Dog));    // true
    Console.WriteLine(a is Dog);                      // true 
}

Dog spot = new Dog(); 
PrintTypes(spot);
string Foo<T>(T parameter) { return typeof(T).Name; }

Animal probably_a_dog = new Dog();
Dog    definitely_a_dog = new Dog();

Foo(probably_a_dog); // this calls Foo<Animal> and returns "Animal"
Foo<Animal>(probably_a_dog); // this is exactly the same as above
Foo<Dog>(probably_a_dog); // !!! This will not compile. The parameter expects a Dog, you cannot pass in an Animal.

Foo(definitely_a_dog); // this calls Foo<Dog> and returns "Dog"
Foo<Dog>(definitely_a_dog); // this is exactly the same as above.
Foo<Animal>(definitely_a_dog); // this calls Foo<Animal> and returns "Animal". 
Foo((Animal)definitely_a_dog); // this does the same as above, returns "Animal"

Context

Stack Overflow Q#983030, score: 2309

Revisions (0)

No revisions yet.