Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

array
length
c#
cio
communitycreator

What is Array.Length in C#?

Theodore Kelechukwu Onyejiaku

Overview

Array.GetLength() in C# is an array property. It is used to get the number of elements, or the length, of an array. It can be used to get the length of an array of any dimension.

Syntax

public int Length { get; }
Syntax for getting the number of array elements in C#

Parameters

None. It is only called on an array.

Return value

The value returned is an integer value that is the total number of elements present in an array. It returns 0 if no elements are present.

Code example

// use System
using System;
// using System.Collections.Generic;

// create class
public class ArrayClearance {
  
    // Main Method
    public static void Main()
    {
  
        // Creating and initializing new the String
        int[] arr1 = {10, 20, 30, 40};
        char[] arr2 = {'E', 'd', 'p', 'r', 'e', 's', 's', 'o'};
        bool[] arr3 = {true, false};
        double[] arr4 = {};
        
        // print lenghts of arrays
        Console.WriteLine(arr1.Length);
        Console.WriteLine(arr2.Length);
        Console.WriteLine(arr3.Length);
        Console.WriteLine(arr4.Length);
    }
}
Get lengths of arrays in C#

Explanation

  • Lines 13-15: We create arrays of different data types.
  • Line 16: We create an empty array.
  • Lines 19-22: We print the lengths of the arrays on the console using the Length property.

RELATED TAGS

array
length
c#
cio
communitycreator

CONTRIBUTOR

Theodore Kelechukwu Onyejiaku
RELATED COURSES

View all Courses

Keep Exploring