Type.GetTypeHandle() Method in C#

The Type.GetTypeHandle() method in C# is used to get the runtime type handle for a specified object. A RuntimeTypeHandle provides a lightweight way to reference a type without holding a direct reference to the Type object itself, which is useful in performance-critical scenarios and interop operations.

Syntax

Following is the syntax for the Type.GetTypeHandle() method −

public static RuntimeTypeHandle GetTypeHandle(object o)

Parameters

  • o − The object for which to get the type handle.

Return Value

Returns a RuntimeTypeHandle structure that represents the type of the specified object.

Using Type.GetTypeHandle() with System Types

Example

using System;

public class Demo {
   public static void Main() {
      Type type1 = typeof(System.Type);
      RuntimeTypeHandle typeHandle = Type.GetTypeHandle(type1);
      Type type = Type.GetTypeFromHandle(typeHandle);
      Console.WriteLine("Attributes = " + type.Attributes);
      Console.WriteLine("Type Referenced = " + type);
   }
}

The output of the above code is −

Attributes = AutoLayout, AnsiClass, Class, Serializable, BeforeFieldInit
Type Referenced = System.RuntimeType

Using Type.GetTypeHandle() with Primitive Types

Example

using System;

public class Demo {
   public static void Main() {
      double value = 42.5;
      RuntimeTypeHandle typeHandle = Type.GetTypeHandle(value);
      Type type = Type.GetTypeFromHandle(typeHandle);
      Console.WriteLine("Attributes = " + type.Attributes);
      Console.WriteLine("Type Referenced = " + type);
      Console.WriteLine("Full Name = " + type.FullName);
   }
}

The output of the above code is −

Attributes = AutoLayout, AnsiClass, Class, Public, SequentialLayout, Sealed, Serializable, BeforeFieldInit
Type Referenced = System.Double
Full Name = System.Double

Using Type.GetTypeHandle() with Custom Objects

Example

using System;

class Student {
   public string Name { get; set; }
   public int Age { get; set; }
}

public class Demo {
   public static void Main() {
      Student student = new Student { Name = "John", Age = 20 };
      RuntimeTypeHandle typeHandle = Type.GetTypeHandle(student);
      Type type = Type.GetTypeFromHandle(typeHandle);
      Console.WriteLine("Type Name = " + type.Name);
      Console.WriteLine("Assembly = " + type.Assembly.GetName().Name);
      Console.WriteLine("Is Class = " + type.IsClass);
   }
}

The output of the above code is −

Type Name = Student
Assembly = testAssembly
Is Class = True

Common Use Cases

  • Performance optimizationRuntimeTypeHandle has less memory overhead than Type objects.

  • Interop scenarios − When working with unmanaged code that requires type handles.

  • Type comparison − Efficient way to compare types without full Type object allocation.

Conclusion

The Type.GetTypeHandle() method provides an efficient way to obtain a RuntimeTypeHandle for any object. This handle can be used for performance-critical operations and can be converted back to a Type object using Type.GetTypeFromHandle() when needed.

Updated on: 2026-03-17T07:04:35+05:30

132 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements