Freigeben über


Type.GetElementType Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

When overridden in a derived class, returns the Type of the object encompassed or referred to by the current array, pointer or reference type.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
Public MustOverride Function GetElementType As Type
public abstract Type GetElementType()

Return Value

Type: System.Type
The Type of the object encompassed or referred to by the current array, pointer, or reference type, or nulla null reference (Nothing in Visual Basic) 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.

Remarks

This method returns nulla null reference (Nothing in Visual Basic) for the Array class.

Examples

The following example demonstrates using the GetElementType method.


Class Example

   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      Dim array As Integer() = {1, 2, 3}
      Dim t As Type = array.GetType()
      Dim t2 As Type = t.GetElementType()
      outputBlock.Text &= String.Format("The element type of {0} is {1}.", _
         array, t2.ToString()) & vbCrLf

      Dim newMe As New Example()
      t = newMe.GetType()
      t2 = t.GetElementType()
      If t2 Is Nothing Then
         outputBlock.Text &= String.Format("The element type of {0} is {1}.", newMe, "Nothing") & vbCrLf
      Else
         outputBlock.Text &= String.Format("The element type of {0} is {1}.", newMe, t2.ToString()) & vbCrLf
      End If
   End Sub 
End Class 

' This code produces output similar to the following:
'
'The element type of System.Int32[] is System.Int32.
'The element type of Example is Nothing.
using System;
class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      int[] array = { 1, 2, 3 };
      Type t = array.GetType();
      Type t2 = t.GetElementType();
      outputBlock.Text += String.Format("The element type of {0} is {1}.", 
         array, t2.ToString()) + "\n";

      Example newMe = new Example();
      t = newMe.GetType();
      t2 = t.GetElementType();
      outputBlock.Text += String.Format("The element type of {0} is {1}.", 
         newMe, t2 == null ? "null" : t2.ToString()) + "\n";
   }
}

/* This code produces the following output:

The element type of System.Int32[] is System.Int32.
The element type of Example is null.
 */

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.