How to use the GetType method of array class in C#?

The GetType() method in C# is inherited from the Object class and returns the exact runtime type of the current instance. When used with arrays, it provides information about the array's type, including its element type and dimensions.

Syntax

Following is the syntax for using the GetType() method −

Type type = arrayInstance.GetType();

Return Value

The method returns a Type object that represents the exact runtime type of the current instance.

Using GetType() with Arrays

When applied to arrays, GetType() returns the array type, which includes information about the element type and array dimensions −

using System;

public class Program {
    public static void Main() {
        int[] intArray = {1, 2, 3, 4, 5};
        string[] stringArray = {"Hello", "World"};
        double[,] twoDArray = {{1.1, 2.2}, {3.3, 4.4}};

        Console.WriteLine("Type of intArray: " + intArray.GetType());
        Console.WriteLine("Type of stringArray: " + stringArray.GetType());
        Console.WriteLine("Type of twoDArray: " + twoDArray.GetType());
    }
}

The output of the above code is −

Type of intArray: System.Int32[]
Type of stringArray: System.String[]
Type of twoDArray: System.Double[,]

Checking Array Element Types

You can use GetType() to determine the element type of an array and perform type checking −

using System;

public class Program {
    public static void Main() {
        object[] values = {100, 17111L, "Hello", 3.14};
        
        foreach (var value in values) {
            Type tp = value.GetType();
            
            if (tp.Equals(typeof(int)))
                Console.WriteLine("{0} is an integer data type.", value);
            else if (tp.Equals(typeof(long)))
                Console.WriteLine("{0} is a long data type.", value);
            else if (tp.Equals(typeof(string)))
                Console.WriteLine("'{0}' is a string data type.", value);
            else
                Console.WriteLine("'{0}' is of type {1}.", value, tp.Name);
        }
    }
}

The output of the above code is −

100 is an integer data type.
17111 is a long data type.
'Hello' is a string data type.
'3.14' is of type Double.

Getting Array Properties

The GetType() method combined with reflection allows you to examine array properties like element type, rank, and whether it's an array −

using System;

public class Program {
    public static void Main() {
        int[] singleDimArray = {1, 2, 3};
        int[,] multiDimArray = {{1, 2}, {3, 4}};
        
        Type type1 = singleDimArray.GetType();
        Type type2 = multiDimArray.GetType();
        
        Console.WriteLine("Single dimension array:");
        Console.WriteLine("IsArray: " + type1.IsArray);
        Console.WriteLine("Element Type: " + type1.GetElementType());
        Console.WriteLine("Array Rank: " + type1.GetArrayRank());
        
        Console.WriteLine("\nMulti dimension array:");
        Console.WriteLine("IsArray: " + type2.IsArray);
        Console.WriteLine("Element Type: " + type2.GetElementType());
        Console.WriteLine("Array Rank: " + type2.GetArrayRank());
    }
}

The output of the above code is −

Single dimension array:
IsArray: True
Element Type: System.Int32
Array Rank: 1

Multi dimension array:
IsArray: True
Element Type: System.Int32
Array Rank: 2

Conclusion

The GetType() method is essential for runtime type checking and reflection in C#. When used with arrays, it provides detailed type information including element types and dimensions, making it useful for dynamic type checking and array analysis.

Updated on: 2026-03-17T07:04:35+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements