Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

array
d
communitycreator

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

Theodore Kelechukwu Onyejiaku

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); 
}
Get the length of an array

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.

RELATED TAGS

array
d
communitycreator

CONTRIBUTOR

Theodore Kelechukwu Onyejiaku
RELATED COURSES

View all Courses

Keep Exploring