Start learning free

LESSON 93 OF 347 · ARRAYS I

Static Array Length in C#: The Length Property

Lesson Read the full explanation

We can get the length of an array using the .length property like this:

int[] ourArray = {50, 40, 30};

// Printing the length:
Console.WriteLine(ourArray.length); // Output: 3

Using the length

We can use the length to access items from the end of an array.

Because arrays are 0-indexed, the index of the last item is length - 1:

let ourArray = [50, 40, 30];

// Printing the last item:
Console.WriteLine(ourArray[ourArray.length - 1]); // Output: 30

// Printing the second to last item:
Console.WriteLine(ourArray[ourArray.length - 2]); // Output: 40

Assignment
Follow the Coding Tutorial and let's play with some arrays.


Hint
Look at the examples above if you get stuck.


STEP 1 OF 3
    Ctrl + Enter to check

    C# · ARRAYS I

    Static Array Length in C#, explained

    Getting the length

    Every array knows how many elements it has. You read that number with the Length property, spelled with a capital L and no parentheses:

    int[] ourArray = {50, 40, 30};
    
    Console.WriteLine(ourArray.Length);  // 3
    

    The length is the number of elements, not the index of the last one. This array has length 3, and its indexes are 0, 1 and 2. The length of an array never changes after it is created.

    Using the length to reach the end

    Because indexes start at 0, the last element is at index Length - 1. The one before it is at Length - 2, and so on. This works for an array of any size, so the code doesn't have to change when the data does:

    int[] ourArray = {50, 40, 30};
    
    Console.WriteLine(ourArray[ourArray.Length - 1]);  // 30
    Console.WriteLine(ourArray[ourArray.Length - 2]);  // 40
    

    C# also has an "index from end" operator: ourArray[^1] is the last element and ourArray[^2] the one before it. It means the same as Length - 1 and Length - 2. The lesson uses the explicit subtraction, which is worth knowing because it works in every language.

    Length in loops

    The most common use of Length is as the limit of a loop over the array. Because the last index is Length - 1, the loop continues while the index is less than the length:

    int[] ages = {14, 32, 50, 21};
    int total = 0;
    
    for (int i = 0; i < ages.Length; i++) {
        total += ages[i];
    }
    Console.WriteLine(total);  // 117
    

    The same loop works for an array of 4 elements or 4,000, and it stays correct if you later add values to the initializer.

    Length of a new array

    For an array created with new int[20], Length is 20 even though every element is still 0. The length counts slots, not meaningful values. An array created with new int[0] or with empty braces has length 0 and is perfectly valid.

    The exercise, step by step

    The starter code declares a string array fruits with three items and an int array ages with four.

    1. Print the length of fruits. fruits.Length is 3. It is a property, so there are no parentheses.
    2. Print the last age. The last index is ages.Length - 1, which is 3, so this prints 21.
    3. Print the second to last age. Subtract one more: ages.Length - 2 is index 2, so this prints 50.
    string[] fruits = {"mango", "kiwi", "grapefruit"};
    int[] ages = {14, 32, 50, 21};
    
    Console.WriteLine(fruits.Length);
    Console.WriteLine(ages[ages.Length - 1]);
    Console.WriteLine(ages[ages.Length - 2]);
    

    The output is:

    3
    21
    50
    

    Common mistakes

    • Using the length as an index. ages[ages.Length] is one past the end and throws an IndexOutOfRangeException. The next lesson looks at this error.
    • Lowercase length. C# is case-sensitive, and the property is Length. Writing ages.length is a compile error. Arrays in JavaScript use lowercase, so this slip is common.
    • Adding parentheses. ages.Length() doesn't compile, because Length is a property, not a method.
    • Empty arrays. An array with no elements has length 0, so arr[arr.Length - 1] asks for index -1 and throws. Check the length before reading the last element.

    Length compared with other sizes

    Do not confuse an array's Length with the length of a string stored in it. For fruits, fruits.Length is 3 because there are three strings, while fruits[0].Length is 5 because "mango" has five characters. Both use the name Length, but they measure different things, so read the expression from left to right to see what is being measured.

    Whenever a line mixes the two, say aloud what each part measures. This habit prevents most of the length-related slips in later chapters, where arrays of strings and arrays of arrays are common.

    Things to try

    For ages from the exercise, ages[ages.Length - 1] - ages[ages.Length - 2] is 21 - 50, which is -29. For an array with a single element, the last index is 0, and for fruits the last element is "grapefruit". Try writing the same expressions with ^1 and ^2 and confirm that the results match.

    Key takeaways

    • array.Length is the number of elements.
    • The last valid index is Length - 1.
    • Use Length - 2, Length - 3 and so on to step back from the end.
    Keep going with C#About 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free