What are the Arrays in C#?

Category: C SharpWhat are the Arrays in C#?
Editor">Editor Staff asked 2 years ago

What are the Arrays in C#?

1 Answers
Editor">Editor Staff answered 2 years ago

n C#, an array index starts at zero. That means the first item of an array starts at the 0th position. The position of the last item on an array will total the number of items – 1. So if an array has 10 items, the last 10th item is in the 9th position. 

 
In C#, arrays can be declared as fixed-length or dynamic.
 
fixed-length array can store a predefined number of items.
 
dynamic array does not have a predefined size. The size of a dynamic array increases as you add new items to the array. You can declare an array of fixed length or dynamic. You can even change a dynamic array to static after it is defined.
 
Let’s take a look at simple declarations of arrays in C#. The following code snippet defines the simplest dynamic array of integer types that do not have a fixed size.
 
int[] intArray;
 
As you can see from the above code snippet, the declaration of an array starts with a type of array followed by a square bracket ([]) and the name of the array.
 
The following code snippet declares an array that can store 5 items only starting from index 0 to 4. 

 

  1. int[] intArray;    
  2. intArray = new int[5];    

The following code snippet declares an array that can store 100 items starting from index 0 to 99. 

 

  1. int[] intArray;    
  2. intArray = new int[100];