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:
using System;
using System.Globalization;
using System.Reflection;
class GFG {
public static void Main()
{
Type type = typeof ( int [,, ]);
Type t = type.GetElementType();
Console.WriteLine( "ElementType is: {0}" , t);
}
}
|
Output:
ElementType is: System.Int32
Example 2:
using System;
using System.Globalization;
using System.Reflection;
class GFG {
public static void Main()
{
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
Share your thoughts in the comments
Please Login to comment...