Why does the indexing start with zero in C# arrays?


Arrays were a pointer to an address in memory of the index. This index was the 1st element of the array. Here, the index is like an offset and the concept even before C language originated.

Let’s say your array elements begins from 0Xff000 and has 5 elements like {35,23,67,88,90}. Therefore, you array in memory would be like the following because int is stored using 4 bytes.

0Xff000 has 35
0Xff004 has 23
0Xff008 has 67
0Xff012 has 88
0Xff016 has 90

That would mean when the array is accessed, zero offsets would be index 0.

Let us further see the concept of zero indexing in C# −

  • If array is empty, it has zero elements and has length 0.
  • If array has one element in 0 index, then it has length 1.
  • If array has two elements in 0 and 1 indexes, then it has length 2.
  • If array has three elements in 0, 1 and 2 indexes, then it has length 3.

The following states that an array in C# begins with index 0 −

/* begin from index 0 */
for ( i = 0; i < 10; i++ ) {
   num[ i ] = i + 10;
}

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 21-Jun-2020

749 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements