Type.GetElementType Method
When overridden in a derived class, returns the Type of the object encompassed or referred to by the current array, pointer or reference type.
[Visual Basic] Public MustOverride Function GetElementType() As Type [C#] public abstract Type GetElementType(); [C++] public: virtual Type* GetElementType() = 0; [JScript] public abstract function GetElementType() : Type;
Return Value
The Type of the object encompassed or referred to by the current array, pointer or reference type.
-or-
A null reference (Nothing in Visual Basic) if the current Type is not an array or a pointer, or is not passed by reference.
Example
[Visual Basic, C#, C++] The following example demonstrates using the GetElementType method.
[Visual Basic] Imports System Class TestGetElementType Public Shared Sub Main() Dim array As Integer() = {1, 2, 3} Dim t As Type = array.GetType() Dim t2 As Type = t.GetElementType() Console.WriteLine("The element type of {0} is {1}.", array, t2.ToString()) Dim newMe As New TestGetElementType() t = newMe.GetType() t2 = t.GetElementType() If t2 Is Nothing Then Console.WriteLine("The element type of {0} is {1}.", newMe, "null") Else Console.WriteLine("The element type of {0} is {1}.", newMe, t2.ToString()) End If End Sub 'Main End Class 'TestGetElementType [C#] using System; class TestGetElementType { public static void Main() { int[] array = {1,2,3}; Type t = array.GetType(); Type t2 = t.GetElementType(); Console.WriteLine("The element type of {0} is {1}.",array, t2.ToString()); TestGetElementType newMe = new TestGetElementType(); t = newMe.GetType(); t2 = t.GetElementType(); Console.WriteLine("The element type of {0} is {1}.", newMe, t2==null? "null" : t2.ToString()); } } [C++] #using <mscorlib.dll> using namespace System; public __gc class TestGetElementType { }; int main() { Int32 array[] = {1, 2, 3}; Type* t = array->GetType(); Type* t2 = t->GetElementType(); Console::WriteLine(S"The element type of {0} is {1}.", array, t2); TestGetElementType* newMe = new TestGetElementType(); t = newMe->GetType(); t2 = t->GetElementType(); Console::WriteLine(S"The element type of {0} is {1}.", newMe, t2==0? S"null" : t2->ToString()); }
[Visual Basic, C#, C++] This code produces the following output:
System.Int32[] element type is System.Int32
TestGetElementType element type is null [JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button
in the upper-left corner of the page.
Requirements
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard
See Also
Type Class | Type Members | System Namespace | HasElementType