How to find the length of an Array in C#

Array.Length Property is utilized to get the absolute number of components in every one of the elements of the Array. Essentially, the length of a cluster is the absolute number of the components which is contained by every one of the elements of that exhibit.

C doesn’t give an underlying method for getting the size of an exhibit. You need to accomplish some stir front and center. I need to make reference to the most straightforward method for doing that, first: saving the length of the exhibit in a variable. Some of the time the straightforward arrangement is the thing that works best.

Program to compute length of exhibit in C utilizing sizeof administrator with steps clarified. Two program models shows utilizing int cluster and burn exhibit.
Array.Length Property is utilized to get the complete number of components in every one of the elements of the Array. Essentially, the length of a cluster is the absolute number of the components which is contained by every one of the elements of that exhibit.

Property Value: This property returns the absolute number of components in every one of the elements of the Array. It can likewise return zero on the off chance that there are no components in the exhibit. The return type is System.Int32.

Exemption: This property tosses the OverflowException on the off chance that the exhibit is multi-faceted and contains more than MaxValue components. Here MaxValue is 2147483647.

Beneath programs represent the utilization of above-examined property:

C# Length of Array

// declares a 1D Array of string. 
string[] weekDays; 
// allocating memory for days. 
weekDays = new string[] {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
// Display length of array.
Console.Write(weekDays.Length);

int prices[5] = { 1, 2, 3, 4, 5 };
You use a variable for the size:
const int SIZE = 5;
int prices[SIZE] = { 1, 2, 3, 4, 5 };
So if you need to iterate the array using a loop, for example, you use that SIZE variable:
for (int i = 0; i < SIZE; i++) {
  printf("%u\n", prices[i]);
}

Example 1:

// C# program to find the
// the total number of
// elements in 1-D Array
using System;
namespace geeksforgeeks {
 
class GFG {
 
    // Main Method
    public static void Main()
    {
 
        // declares a 1D Array of string.
        string[] weekDays;
 
        // allocating memory for days.
        weekDays = new string[] {"Sun", "Mon", "Tue", "Wed",
                                       "Thu", "Fri", "Sat"};
 
        // Displaying Elements of the array
        foreach(string day in weekDays)
            Console.Write(day + " ");
 
        Console.Write("\nTotal Number of Elements: ");
 
        // using Length property
        Console.Write(weekDays.Length);
    }
}
}
Output:

Sun Mon Tue Wed Thu Fri Sat 
Total Number of Elements: 7

Example 2:

// C# program to find the total
// number of elements in the
// multidimensional Arrays
using System;
namespace geeksforgeeks {
 
class GFG {
 
    // Main Method
    public static void Main()
    {
 
        // Two-dimensional array
        int[, ] intarray = new int[, ] {{1, 2},
                                        {3, 4},
                                        {5, 6},
                                        {7, 8}};
 
        // The same array with dimensions
        // specified 4 row and 2 column.
        int[, ] intarray_d = new int[4, 2] {{ 1, 2}, 
                                             {3, 4}, 
                                             {5, 6},
                                             {7, 8}};
 
        // Three-dimensional array.
        int[,, ] intarray3D = new int[,, ] {{{ 1, 2, 3},
                                            { 4, 5, 6}},
                                            {{ 7, 8, 9},
                                         {10, 11, 12}}};
 
        // The same array with dimensions
        // specified 2, 2 and 3.
        int[,, ] intarray3Dd = new int[2, 2, 3] {{{1, 2, 3},
                                                 {4, 5, 6}},
                                                 {{ 7, 8, 9},
                                              {10, 11, 12}}};
 
        Console.Write("Total Number of Elements in intarray: ");
 
        // using Length property
        Console.Write(intarray.Length);
 
        Console.Write("\nTotal Number of Elements in intarray_d: ");
 
        // using Length property
        Console.Write(intarray_d.Length);
 
        Console.Write("\nTotal Number of Elements in intarray3D: ");
 
        // using Length property
        Console.Write(intarray3D.Length);
 
        Console.Write("\nTotal Number of Elements in intarray3Dd: ");
 
        // using Length property
        Console.Write(intarray3Dd.Length);
    }
}
}
Output:

Total Number of Elements in intarray: 8
Total Number of Elements in intarray_d: 8
Total Number of Elements in intarray3D: 12
Total Number of Elements in intarray3Dd: 12

Also Read: What is VLSI IC technology and Y chart?

Leave a Reply

Your email address will not be published. Required fields are marked *