C# | Type.GetElementType() Method

Type.GetElementType() Method is used to return the Type of the object encompassed or referred to by the current array, pointer or reference type when overridden in a derived class.

Syntax: public abstract Type GetElementType ();

Return Value: This method returns the Type of the object encompassed or referred to by the current array, pointer, or reference type, or null if the current Type is not an array or a pointer, or is not passed by reference, or represents a generic type or a type parameter in the definition of a generic type or generic method.

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

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the
// Type.GetElementType() Method
using System;
using System.Globalization;
using System.Reflection;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        // Declaring and initializing type
        Type type = typeof(int[,, ]);
  
        // using GetElementType() Method
        Type t = type.GetElementType();
  
        // Display the ElementType
        Console.WriteLine("ElementType is: {0}", t);
    }
}

chevron_right


Output:

ElementType is: System.Int32

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the
// Type.GetElementType() Method
using System;
using System.Globalization;
using System.Reflection;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        // Creating the object of class
        GFG obj = new GFG();
  
        Type typ1 = obj.GetType();
        Type typ2 = typ1.GetElementType();
  
        Console.WriteLine("Element type of {0} is {1}", obj, 
                      typ2==null? "null" : typ2.ToString());
    }
  
}

chevron_right


Output:

Element type of GFG is null

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.