Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Typeof() vs GetType() in C#
In C#, both typeof() and GetType() are used to obtain type information, but they work differently. typeof() is a compile-time operator that returns the Type object for a specified type, while GetType() is a runtime method that returns the actual type of an object instance.
Syntax
Following is the syntax for using typeof() operator −
Type type = typeof(TypeName);
Following is the syntax for using GetType() method −
Type type = objectInstance.GetType();
Using typeof() Operator
The typeof() operator takes a type name as its argument and returns the Type object representing that type. It works at compile-time and can be used with any type name.
Example
using System;
class Program {
static void Main() {
Console.WriteLine(typeof(int));
Console.WriteLine(typeof(byte));
Console.WriteLine(typeof(string));
Console.WriteLine(typeof(Program));
}
}
The output of the above code is −
System.Int32 System.Byte System.String Program
Using GetType() Method
The GetType() method is called on an object instance and returns the actual runtime type of that object. This is particularly useful for polymorphic scenarios where the compile-time type differs from the runtime type.
Example
using System;
class Program {
public static void Main() {
object[] values = { (int) 100, (long) 17111, "Hello" };
foreach (var value in values) {
Type tp = value.GetType();
if (tp.Equals(typeof(int)))
Console.WriteLine("{0} is an integer data type.", value);
else if (tp.Equals(typeof(long)))
Console.WriteLine("{0} is a long data type.", value);
else
Console.WriteLine("'{0}' is of type {1}.", value, tp.Name);
}
}
}
The output of the above code is −
100 is an integer data type. 17111 is a long data type. 'Hello' is of type String.
Polymorphism Example
The difference between typeof() and GetType() becomes clear in inheritance scenarios −
using System;
class Animal {
public virtual void Speak() {
Console.WriteLine("Animal speaks");
}
}
class Dog : Animal {
public override void Speak() {
Console.WriteLine("Dog barks");
}
}
class Program {
static void Main() {
Animal myAnimal = new Dog();
Console.WriteLine("typeof(Animal): " + typeof(Animal));
Console.WriteLine("myAnimal.GetType(): " + myAnimal.GetType());
Console.WriteLine("Are they equal? " + (typeof(Animal) == myAnimal.GetType()));
}
}
The output of the above code is −
typeof(Animal): Animal myAnimal.GetType(): Dog Are they equal? False
Comparison
| typeof() | GetType() |
|---|---|
| Compile-time operator | Runtime method |
| Takes type name as argument | Called on object instance |
| Cannot be used with null values | Throws NullReferenceException on null |
| Returns compile-time type | Returns actual runtime type |
Conclusion
Use typeof() when you know the type at compile-time and want to get its Type object. Use GetType() when you need to determine the actual runtime type of an object instance, especially in polymorphic scenarios where the runtime type may differ from the declared type.
