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
C# Object.GetType() Method with Examples
The Object.GetType() method in C# is used to get the runtime type of the current instance. This method is inherited by all types in C# since every type derives from Object. It returns a Type object that contains metadata about the type.
Syntax
Following is the syntax for the GetType() method −
public Type GetType();
Return Value
The method returns a Type object that represents the exact runtime type of the current instance.
Using GetType() with Built-in Types
The GetType() method can be called on any object to determine its type at runtime −
using System;
public class Demo {
public static void Main() {
Object ob = new Object();
String str = "Jim";
int number = 42;
Type type1 = ob.GetType();
Type type2 = str.GetType();
Type type3 = number.GetType();
Console.WriteLine("Type = " + type1);
Console.WriteLine("Type = " + type2);
Console.WriteLine("Type = " + type3);
Console.WriteLine("\nHash Code = " + type1.GetHashCode());
Console.WriteLine("Hash Code = " + type2.GetHashCode());
Console.WriteLine("Hash Code = " + type3.GetHashCode());
}
}
The output of the above code is −
Type = System.Object Type = System.String Type = System.Int32 Hash Code = 30015890 Hash Code = 21083178 Hash Code = 43942917
Using GetType() with Custom Types
You can also use GetType() with custom classes and structs to get their type information −
using System;
public struct Value {
private int v1;
private int v2;
private int v3;
public Value(int v1, int v2, int v3) {
this.v1 = v1;
this.v2 = v2;
this.v3 = v3;
}
public override int GetHashCode() {
return Tuple.Create(v1, v2, v3).GetHashCode();
}
}
public class Demo {
public static void Main() {
Value v = new Value(1, 7, 12);
Console.WriteLine("Hash Code: " + v.GetHashCode());
Console.WriteLine("Type: " + v.GetType());
v = new Value(12, 8, 7);
Console.WriteLine("Hash Code: " + v.GetHashCode());
Console.WriteLine("Type: " + v.GetType());
}
}
The output of the above code is −
Hash Code: 1258 Type: Value Hash Code: 12803 Type: Value
Type Comparison Using GetType()
The GetType() method is commonly used for type checking and comparison −
using System;
public class Animal { }
public class Dog : Animal { }
public class Demo {
public static void Main() {
Animal animal = new Dog();
Dog dog = new Dog();
Console.WriteLine("animal.GetType(): " + animal.GetType());
Console.WriteLine("dog.GetType(): " + dog.GetType());
Console.WriteLine("Same type? " + (animal.GetType() == dog.GetType()));
Console.WriteLine("Is Dog type? " + (animal.GetType() == typeof(Dog)));
Console.WriteLine("Is Animal type? " + (animal.GetType() == typeof(Animal)));
}
}
The output of the above code is −
animal.GetType(): Dog dog.GetType(): Dog Same type? True Is Dog type? True Is Animal type? False
Conclusion
The GetType() method is essential for runtime type identification in C#. It returns the exact type of an object, which is useful for type checking, reflection, and debugging. Unlike typeof() which works with compile-time types, GetType() provides the actual runtime type of an instance.
