Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type Checking: typeof, GetType, or is?

I've seen many people use the following code:

Type t = typeof(obj1); 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?

like image 767
jasonh Avatar asked Jun 11 '09 19:06

jasonh


People also ask

What is the difference between GetType and typeof?

typeof keyword takes the Type itself as an argument and returns the underline Type of the argument whereas GetType() can only be invoked on the instance of the type.

What is GetType?

GetType Method is used to find the type of the current instance. This method returns the instances of the Type class that are used for consideration. Syntax: public Type GetType (); Return Value: This method return the exact runtime type of the current instance.

What is return type of GetType ()?

The C# GetType() method is used to get type of current object. It returns the instance of Type class which is used for reflection.

What is the use of typeof ()?

typeof is an operator to obtain a type known at compile-time (or at least a generic type parameter). The operand of typeof is always the name of a type or type parameter - never an expression with a value (e.g. a variable). See the C# language specification for more details.


2 Answers

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>(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" 
like image 197
Jimmy Avatar answered Oct 11 '22 06:10

Jimmy


Use typeof when you want to get the type at compilation time. Use GetType when you want to get the type at execution time. There are rarely any cases to use is as it does a cast and, in most cases, you end up casting the variable anyway.

There is a fourth option that you haven't considered (especially if you are going to cast an object to the type you find as well); that is to use as.

Foo foo = obj as Foo;  if (foo != null)     // your code here 

This only uses one cast whereas this approach:

if (obj is Foo)     Foo foo = (Foo)obj; 

requires two.

Update (Jan 2020):

  • As of C# 7+, you can now cast inline, so the 'is' approach can now be done in one cast as well.

Example:

if(obj is Foo newLocalFoo) {     // For example, you can now reference 'newLocalFoo' in this local scope     Console.WriteLine(newLocalFoo); } 
like image 22
Andrew Hare Avatar answered Oct 11 '22 07:10

Andrew Hare