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
What is typeof, GetType or is in C#?
C# provides three important operators for working with types: typeof, GetType(), and is. These operators allow you to examine object types at runtime, perform type checking, and enable reflection-based operations.
Syntax
Following is the syntax for the typeof operator −
Type type = typeof(ClassName);
Following is the syntax for the GetType() method −
Type type = objectInstance.GetType();
Following is the syntax for the is operator −
bool result = objectInstance is TargetType;
Understanding typeof, GetType, and is
Using typeof Operator
The typeof operator gets the Type object for a specific type at compile time. It takes a type name as a parameter and returns a Type object.
Example
using System;
class Demo {
}
class Program {
static void Main() {
Type stringType = typeof(string);
Type demoType = typeof(Demo);
Type intType = typeof(int);
Console.WriteLine("typeof(string): " + stringType.Name);
Console.WriteLine("typeof(Demo): " + demoType.Name);
Console.WriteLine("typeof(int): " + intType.Name);
}
}
The output of the above code is −
typeof(string): String typeof(Demo): Demo typeof(int): Int32
Using GetType() Method
The GetType() method returns the exact runtime type of an object instance. It is called on an object and returns a Type object representing the actual type of that instance.
Example
using System;
class Animal {
}
class Dog : Animal {
}
class Program {
static void Main() {
Animal animal = new Dog();
string text = "Hello World";
int number = 42;
Console.WriteLine("animal.GetType(): " + animal.GetType().Name);
Console.WriteLine("text.GetType(): " + text.GetType().Name);
Console.WriteLine("number.GetType(): " + number.GetType().Name);
}
}
The output of the above code is −
animal.GetType(): Dog text.GetType(): String number.GetType(): Int32
Using is Operator
The is operator checks if an object is compatible with a specific type. It returns true if the object can be cast to the specified type, and false otherwise. It also works with inheritance and interface implementations.
Example
using System;
class Animal {
}
class Dog : Animal {
}
class Program {
static void Main() {
Animal animal = new Dog();
Dog dog = new Dog();
string text = "Hello";
Console.WriteLine("dog is Dog: " + (dog is Dog));
Console.WriteLine("dog is Animal: " + (dog is Animal));
Console.WriteLine("animal is Dog: " + (animal is Dog));
Console.WriteLine("text is string: " + (text is string));
Console.WriteLine("text is Animal: " + (text is Animal));
}
}
The output of the above code is −
dog is Dog: True dog is Animal: True animal is Dog: True text is string: True text is Animal: False
Comparison of typeof, GetType, and is
| Operator | When to Use | Return Type | Works At |
|---|---|---|---|
| typeof | When you need the Type object of a known type | Type | Compile-time |
| GetType() | When you need the actual runtime type of an object | Type | Runtime |
| is | When you need to check type compatibility | bool | Runtime |
Combined Example
using System;
class Demo {
}
class Program {
static void Main() {
var demo = new Demo();
Console.WriteLine("typeof: " + typeof(Demo));
Type tp = demo.GetType();
Console.WriteLine("GetType: " + tp);
if (demo is Demo) {
Console.WriteLine("is keyword check: true");
}
Console.WriteLine("Are types equal: " + (typeof(Demo) == demo.GetType()));
}
}
The output of the above code is −
typeof: Demo GetType: Demo is keyword check: true Are types equal: True
Conclusion
The typeof operator gets type information at compile time, GetType() returns the runtime type of an object instance, and is checks type compatibility. Use typeof for known types, GetType() for runtime type discovery, and is for safe type checking before casting.
