Type.IsArrayImpl Method

Definition

When overridden in a derived class, implements the IsArray property and determines whether the Type is an array.

protected abstract bool IsArrayImpl ();

Returns

true if the Type is an array; otherwise, false.

Examples

The following example overrides the IsArrayImpl method in the MyTypeDelegator class, checks if a variable is an array, and displays the result.

using System;
using System.Reflection;
public class MyTypeDelegator : TypeDelegator
{
    public string myElementType = null;
    public Type myType;
    public MyTypeDelegator(Type myType) : base(myType)
    {
        this.myType = myType;
    }
    // Override IsArrayImpl().
    protected override bool IsArrayImpl()
    {
        // Determine whether the type is an array.
        if(myType.IsArray)
        {
            myElementType = "array";
            return true;
        }
        // Return false if the type is not an array.
        return false;
    }
}
public class Type_IsArrayImpl
{
    public static void Main()
    {
        try
        {
            int myInt = 0 ;
            // Create an instance of an array element.
            int[] myArray = new int[5];
            MyTypeDelegator myType = new MyTypeDelegator(myArray.GetType());
            Console.WriteLine("\nDetermine whether the variable is an array.\n");
            // Determine whether myType is an array type.
            if( myType.IsArray)
                Console.WriteLine("The type of myArray is {0}.", myType.myElementType);
            else
                Console.WriteLine("myArray is not an array.");
            myType = new MyTypeDelegator(myInt.GetType());

            // Determine whether myType is an array type.
            if( myType.IsArray)
                Console.WriteLine("The type of myInt is {0}.", myType.myElementType);
            else
                Console.WriteLine("myInt is not an array.");
        }
        catch( Exception e )
        {
            Console.WriteLine("Exception: {0}", e.Message );
        }
    }
}

Remarks

An instance of the Array class must return false because it's an object, not an array.

Applies to

Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

See also