C# | Type.GetTypeHandle() Method

Type.GetTypeHandle() Method is used to get the handle for the Type of a specified object.

Syntax: public static RuntimeTypeHandle GetTypeHandle (object o);
Here, it takes the object for which to get the type handle.

Return Value: This method returns The handle for the Type of the specified Object.



Exception: This method throws ArgumentNullException if o is null.

Below programs illustrate the use of Type.GetTypeHandle() Method:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the
// Type.GetTypeHandle() Method
using System;
using System.Globalization;
using System.Reflection;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
  
        // try-catch block for handling Exception
        try {
  
            // creating and initializing object Type
            Type type = typeof(int);
  
            // getting handle of given type
            // by using GetTypeHandle() Method
            RuntimeTypeHandle handle = Type.GetTypeHandle(type);
  
            // Display the Result
            Console.WriteLine("Type referenced is {0}", handle);
        }
  
        // catch ArgumentNullException here
        catch (ArgumentNullException e) 
        {
            Console.WriteLine("object is null.");
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
}

chevron_right


Output:

Type referenced is System.RuntimeTypeHandle

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the
// Type.GetTypeHandle() Method
using System;
using System.Globalization;
using System.Reflection;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
  
        // try-catch block for handling Exception
        try {
  
            // creating and initializing object Type
            Type type = typeof(int);
  
            // getting handle of given type
            // by using GetTypeHandle() Method
            RuntimeTypeHandle handle = Type.GetTypeHandle(null);
  
            // Display the Result
            Console.WriteLine("Type referenced is {0}", handle);
        }
  
        // catch ArgumentNullException here
        catch (ArgumentNullException e)
        {
            Console.WriteLine("object is null.");
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
}

chevron_right


Output:

object is null.
Exception Thrown: System.ArgumentNullException

Reference:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.