c sharp

C# IsArray Property

Working with arrays is a good approach because we can store multiple values at a time and process them. Many applications like searching.sorting techniques are implemented on these kinds of data structures. So, it is important to check whether the given variable is an array or not.

IsArray Property

In C#, IsArray property is used to check if the given type is an array or not. If the variable type is an array, it returns True; else False.

Syntax:

variable.IsArray

 
Return Type:

It returns the Boolean value (True/False).

In C#, an array is created by specifying the data type and size.

Syntax:

type[] array_name = new type[size];

 

Example 1:

Here, we are creating arrays with different data types and checking if they are arrays or not.

using System;
class Linuxhint
{
        static public  void Main(){

    //create an array with size 2 of integer type
    int[] first_array = new int[2];
   
     //create an array with size 2 of double type
    double[] second_array = new double[2];
   
     //create an array with size 2 of float type
    float[] third_array = new float[2];
   
     //create an array with size 2 of string type
    string[] fourth_array = new string[2];
   
    //create an array with size 2 of long type
    long[] fifth_array = new long[2];
   
    //create an array with size 2 of short type
    short[] sixth_array = new short[2];
   
    // Check the first_array is array or not
    Console.WriteLine("Integer Type Array: "+first_array.GetType().IsArray);
   
    // Check the second_array is array or not
    Console.WriteLine("Double Type Array: "+second_array.GetType().IsArray );
   
    // Check the third_array is array or not
    Console.WriteLine("Float Type Array: "+third_array.GetType().IsArray);
   
    // Check the fourth_array is array or not
    Console.WriteLine("String Type Array: "+fourth_array.GetType().IsArray);
   
    // Check the fifth_array is array or not
    Console.WriteLine("Long Type Array: "+fifth_array.GetType().IsArray);
   
     // Check the sixth_array is array or not
    Console.WriteLine("Short Type Array: "+sixth_array.GetType().IsArray);
}
}

 
Output:


Explanation:

Inside Main method –

Line 8 – Line 23, we created six types of arrays with size – 2 specified in the below image:


Line 26 – Line 41, we are checking if the above-created variables are array types or not.


Since all six variables are array types, it returned True.

Example 2:

Here, we are creating arrays with different data types and check if they are arrays or not.

using System;
class Linuxhint
{
    static public  void Main(){

    //create a variable with integer type
    int first_ = 12;
   
     //create a variable with double type
    double  second_=6.9877;
   
     //create a variable with float type
    float  third_ = 45.667F;
   
     //create a variable with string type
    string  fourth_ = "Linux Hint";
   
    //create a variable with long type
    long fifth_ = 455454;
   
    //create a variable with short type
    short  sixth_ = 1;
   
    // Check the integer type variable is array or not
    Console.WriteLine("Integer Variable: "+first_.GetType().IsArray);
   
    // Check the Double type variable is array or not
    Console.WriteLine("Double Variable: "+second_.GetType().IsArray );
   
    // Check the Float type variable is array or not
    Console.WriteLine("Float Variable: "+third_.GetType().IsArray);
   
    // Check the String type variable is array or not
    Console.WriteLine("String Variable: "+fourth_.GetType().IsArray);
   
    //Check the Long type variable is array or not
    Console.WriteLine("Long Variable: "+fifth_.GetType().IsArray);
   
     // Check the Short type variable is array or not
    Console.WriteLine("Short Variable: "+sixth_.GetType().IsArray);
}
}

 
Output:


Explanation:

Inside Main method –

Line 7 – Line 22, we created six types of variables with values assigned to them.


Line 25 – Line 40, we are checking if the above-created variables are arrays or not.


Since all six variables are not arrays, it returned False.

Summary

In this C# tutorial, we saw how to check if the variable type is an array or not using IsArray property. This property tells us by returning a Boolean value. If it is True, we can say that the variable is an array and if it returns False, we can say that the variable is not an array.

About the author

Gottumukkala Sravan Kumar

B tech-hon's in Information Technology; Known programming languages - Python, R , PHP MySQL; Published 500+ articles on computer science domain