Arrays are zero based, fixed size collection of elements, meaning first index of an Array is 0, and once we initialize an array it’s size cannot be changed. Array is an reference type. System.Array class holds static and instance methods that we can use with arrays.
Declaration
Arrays can can contain reference or value types. We declare an Array with type followed by [] (square brackets) and variable:
1 2 3 4 5 6 7 |
string[] cityNames; int[] arrayOfIntegers; DateTime[] importantDates; Employe[] arrayOfEmployees; |
Initialization
As Arrays are an reference type we initialize it by assigning Array variable to Array instance but instead of using round brackets after instance type we use square brackets which should hold integer value. That value is specifying size of our Array and that value cannot be changed. If input number 3 inside square brackets, first index will be 0 and last will be 2. We can also use var keyword while initializing an Array.
1 2 3 4 5 6 7 8 9 |
string[] cityNames = new string[1]; int[] arrayOfIntegers = new int[2]; DateTime[] importantDates = new DateTime[3]; Employe[] arrayOfEmployees = new Employee[4]; var flowers = new string[5]; |
Populating an array with values
Example shows array of city names that can hold 3 elements and multiple approaches to populating an array with values:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
//Approach 1 string[] cityNames = new string[5]; cityNames[0] = "London"; cityNames[1] = "New York"; cityNames[2] = "Zadar"; //Approach 2 string[] cityNames = new string[3] { "London", "New York", "Zadar" }; //Approach 3 string[] cityNames = { "London", "New York", "Zadar" }; |
How to use arrays
Retrieve value
We retrieve an element from array by specifying index of that value. If for example we wanted to retrieve first element from our previous array we can access that value like this:
1 2 3 |
Console.WriteLine(cityNames[0]); var cityName = cityNames.GetValue(1); |
Approach 1 is commonly known as manual initializer, and Approaches 2 and 3 are known as collection initializers.
Iterate through array
We can iterate an arrays using for and foreach statements
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
foreach (string cityName in cityNames) { Console.WriteLine("City name is {0}", cityName); } for (int i = 0; i < cityNames.Length; i++) { Console.WriteLine("City name is {0}", cityNames[i]); } |
Find index of an value
1 |
int zadarIndex = Array.IndexOf(cityNames, "Zadar"); |
Change value of an element
1 |
cityNames.SetValue("Tokyo", 1); |
Detect if array holdes speciffic value
1 |
bool newYorkExists = cityNames.Contains("Zadar"); |
Lenght of an array
1 |
int arrayLenght = cityNames.Length; |
FAQ:
Q:What will happen if we try to add more element to an array than array can hold?
A: Compiler warning will be received.
Q:What will happen if we try to add less element to an array than array can hold?
A: Not populated elements will be null.
Q: What will happen if we try access element of an array that is out of scope of an array?
A: IndexOutOfRangeException will occur.
Q: What will happen if we try to find index value of an element if that element does not exist inside of an array?
A: You will get value -1.
Good practices for using arrays:
Use plural variable name. Variable name cityNames is better naming convention than cityName.
Use arrays only if you know array size during design time because array size cannot be changed during runtime.
Populate your arrays with collection initializer technique because they are less error prone and easier to read.