How to get the length of an array in the D programming Language

Overview

We can use the length static property of an array to get its length. The length of an array is the total number of elements that an array contains.

Syntax

array.length

Return value

The value returned is an integer that represents the number of elements that the array contains.

Code example

// import std.stdio
import std.stdio;
// main method
void main(string[] args) {
// create an array
int[3] ages;
// add values
ages[0] = 18;
ages[1] = 34;
ages[2] = 50;
// print the length of the array
writeln(ages.length);
}

Code explanation

  • Line 2: We import the std library.
  • Line 5: We create the main method.
  • Lines 9–11: We give values to the elements of the array.
  • Line 14: We call the sizeof property on the array. Then, we print the result to the console.

Free Resources