0 out of 1 rated this helpful - Rate this topic

Array.Length Property

Gets a 32-bit integer that represents the total number of elements in all the dimensions of the Array.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)
public int Length { get; }

Property Value

Type: System.Int32
A 32-bit integer that represents the total number of elements in all the dimensions of the Array; zero if there are no elements in the array.

Retrieving the value of this property is an O(1) operation.

The following code example demonstrates methods to get the length of an array.


using System;

public class SamplesArray
{
    public static void Main()
    {
        // make a single dimension array
        Array MyArray1 = Array.CreateInstance(typeof(int), 5);

        // make a 3 dimensional array
        Array MyArray2 = Array.CreateInstance(typeof(int), 5, 3, 2);

        // make an array container
        Array BossArray = Array.CreateInstance(typeof(Array), 2);
        BossArray.SetValue(MyArray1, 0);
        BossArray.SetValue(MyArray2, 1);

        int i = 0, j, rank;
        foreach (Array anArray in BossArray)
        {
            rank = anArray.Rank;
            if (rank > 1)
            {
                Console.WriteLine("Lengths of {0:d} dimension array[{1:d}]", rank, i);
                // show the lengths of each dimension
                for (j = 0; j < rank; j++)
                {
                    Console.WriteLine("    Length of dimension({0:d}) = {1:d}", j, anArray.GetLength(j));
                }
            }
            else
            {
                Console.WriteLine("Lengths of single dimension array[{0:d}]", i);
            }
            // show the total length of the entire array or all dimensions
            Console.WriteLine("    Total length of the array = {0:d}", anArray.Length);
            Console.WriteLine();
            i++;
        }
    }
}

/*
This code produces the following output:

Lengths of single dimension array[0]
    Total length of the array = 5

Lengths of 3 dimension array[1]
    Length of dimension(0) = 5
    Length of dimension(1) = 3
    Length of dimension(2) = 2
    Total length of the array = 30
*/


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Frequency count of array values

Create a C# Windows Forms Application project with a Button and a RichTextBox and add this code:

private Dictionary<string, int> arrCount(string[] arr, Dictionary<string, int> dic)
{
    foreach (var item in arr)
    {
        if (!dic.ContainsKey(item))
        {
            dic.Add(item, 1);
        }
        else
        {
            dic[item]++;
        }
    }
    return dic;
}
private void button1_Click(object sender, EventArgs e)
{
    string[] arr = { "count", "me", "and", "me", "and", "me", "as", "well" };
    Dictionary<string, int> dic = new Dictionary<string, int>();
    dic = arrCount(arr, dic);
    StringBuilder sb = new StringBuilder();
    foreach (KeyValuePair<string, int> pair in dic)
    {
        sb.AppendLine(string.Format("{0} {1}", pair.Key, pair.Value));
    }
    richTextBox1.Text = sb.ToString();
    return;
}

Press F5 and click on the Button. This text should appear in the textbox:

    count 1
    me 3
    and 2
    as 1
    well 1

Jagged arrays
The Length property behaves as described for declared multidimensional arrays. For jagged arrays, the Length property returns the number of arrays contained in the top dimension.