Open In App

C# | Type.GetElementType() Method

Improve
Improve
Like Article
Like
Save
Share
Report

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:




// 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);
    }
}


Output:

ElementType is: System.Int32

Example 2:




// 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());
    }
  
}


Output:

Element type of GFG is null

Reference:



Last Updated : 09 May, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads