Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Type.GetElementType() Method in C#
The Type.GetElementType() method in C# is used to return the Type of the object encompassed or referred to by the current array, pointer, or reference type. This method is particularly useful when working with collection types to discover what type of elements they contain.
Syntax
Following is the syntax −
public abstract Type GetElementType();
Return Value
Returns a Type object representing the element type of the current array, pointer, or reference type. If the current Type is not an array, pointer, or reference type, the method returns null.
Using GetElementType() with Arrays
Example
Let us see an example to implement the Type.GetElementType() method with a string array −
using System;
public class Demo {
public static void Main() {
string[] arr = {"tom", "amit", "kevin", "katie"};
Type t1 = arr.GetType();
Type t2 = t1.GetElementType();
Console.WriteLine("Array Type = " + t1.ToString());
Console.WriteLine("Element Type = " + t2.ToString());
}
}
The output of the above code is −
Array Type = System.String[] Element Type = System.String
Using GetElementType() with Multidimensional Arrays
Example
Let us see another example with a multidimensional array type −
using System;
public class Demo {
public static void Main() {
Type t1 = typeof(int[,,,, ]);
Type t2 = t1.GetElementType();
Console.WriteLine("Array Type = " + t1.ToString());
Console.WriteLine("Element Type = " + t2.ToString());
Console.WriteLine("Array Rank = " + t1.GetArrayRank());
}
}
The output of the above code is −
Array Type = System.Int32[,,,,] Element Type = System.Int32 Array Rank = 5
Using GetElementType() with Different Types
Example
This example demonstrates how GetElementType() behaves with different types including non-array types −
using System;
public class Demo {
public static void Main() {
// Array types
Type intArrayType = typeof(int[]);
Type doubleArrayType = typeof(double[,]);
// Non-array type
Type stringType = typeof(string);
Console.WriteLine("int[] element type: " + intArrayType.GetElementType());
Console.WriteLine("double[,] element type: " + doubleArrayType.GetElementType());
// This will return null since string is not an array type
Type nonArrayResult = stringType.GetElementType();
Console.WriteLine("string element type: " + (nonArrayResult?.ToString() ?? "null"));
}
}
The output of the above code is −
int[] element type: System.Int32 double[,] element type: System.Double string element type: null
Common Use Cases
-
Generic Type Analysis − Discovering the element type of arrays at runtime for dynamic operations.
-
Reflection Operations − Building generic methods that work with arrays of any element type.
-
Serialization − Understanding the structure of array types for proper serialization and deserialization.
Conclusion
The Type.GetElementType() method is essential for runtime type analysis of arrays, pointers, and reference types. It returns the element type for array types and null for non-array types, making it useful for reflection-based operations and generic programming scenarios.
