Get the Length of an Array in C#

This article will introduce a method to find the length of an array in C#.

Use the Array.Length Property to Find the Length of an Array in C

In C#, we can use the Array.Length property to find the length of an array. This property finds the total number of elements present in an array. The correct syntax to use this property is as follows.

ArrayName.Length;

This property returns the length of the array.

The program below shows how we can use the Array.Length property to find an array’s length.

using System; 

namespace FindLength { 
class Length { 
    public static void Main() 
    { 
        string[] flowers; 
        flowers = new string[] {"Rose", "Jasmine", "Lili", "Hibiscus", 
                                    "Tulip", "Sun Flower"}; 
        foreach(string flower in flowers) 
            Console.Write(flower + "\n"); 
        Console.Write("\nThe Number of Elements in the Array is: "); 
        Console.Write(flowers.Length); 
    } 
} 
} 

Output:

Rose
Jasmine
Lili
Hibiscus
Tulip
Sun Flower

The Number of Elements in the Array is: 6
Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - Csharp Array

  • Sort an Array in C#