Int
arraysInt
arrays are common: they store many integer values. These values can be used in many ways—ints can refer to indexes in other collections, or measurements from the real world.
Elements in an int
array are stored together. This improves locality of reference when looping over the values—loops are fast on int
arrays.
This example shows how to declare int
arrays. The first 3 are declared on single lines, while the fourth array is declared by individual assignment of the elements.
using System; // Declare int arrays. int[] arr1 = new int[] { 3, 4, 5 }; int[] arr2 = { 3, 4, 5 }; var arr3 = new int[] { 3, 4, 5 }; // Declare int array of zeros. int[] arr4 = new int[3]; arr4[0] = 3; arr4[1] = 4; arr4[2] = 5; if (arr1[0] == arr2[0] && arr1[0] == arr3[0] && arr1[0] == arr4[0]) { Console.WriteLine("First elements are the same"); }First elements are the same
Here we see two ways to create an int
array with zero elements. An empty initializer expression can be used. Or we can specify a length of 0.
using System; // This is a zero-element int array. var values1 = new int[] { }; Console.WriteLine(values1.Length); // This is a zero-element int array also. var values2 = new int[0]; Console.WriteLine(values2.Length);0 0
We can use int
arrays in loops. The example shows the foreach
-loop, which has simpler syntax, and the for
-loop, which gives you more control and possibly greater performance.
Main
method calls the GetEmployeeIds
method twice. The ints in the int
array are looped over with foreach
.GetEmployeeIds
shows how to return an int
array from a method in the C# language.int
array result—it is garbage-collected.using System; class Program { static void Main() { // Loop over array of integers. foreach (int id in GetEmployeeIds()) { Console.WriteLine(id); } // Loop over array of integers. int[] employees = GetEmployeeIds(); for (int i = 0; i < employees.Length; i++) { Console.WriteLine(employees[i]); } } /// <summary> /// Returns an array of integers. /// </summary> static int[] GetEmployeeIds() { int[] employees = new int[5]; employees[0] = 1; employees[1] = 3; employees[2] = 5; employees[3] = 7; employees[4] = 8; return employees; } }1 3 5 7 8 1 3 5 7 8
Next we use int
arrays in an object-oriented program design. The Employee class
here stores an internal reference to an int
array at the class
level.
int
array with 3 elements. This stores the team numbers that the employee belongs to.int
array here exists only in one place. The Employee constructor accepts a reference to an int
array.using System; class Program { static void Main() { // Declare new int array. int[] teams = new int[3]; teams[0] = 1; teams[1] = 2; teams[2] = 3; // New employee that stores int array reference. Employee employee = new Employee(teams); // Loop through each int in employee's class. foreach (int team in employee.Teams) { Console.WriteLine(team); } } } /// <summary> /// Stores an employee and his teams. /// </summary> class Employee { /// <summary> /// Int array reference at class level. /// </summary> int[] _teams; /// <summary> /// Create new employee. /// </summary> /// <param name="teams">Teams for the employee.</param> public Employee(int[] teams) { _teams = teams; } /// <summary> /// Get array of teams. /// </summary> public int[] Teams { get { return _teams; } } }1 2 3
An array in the C# language is a reference type. This means it refers to another object and doesn't contain the raw data.
We used int
arrays in a C# program. We declared int
arrays and then tested individual elements. Int
arrays can also be used as parameters and return values.