Professional Documents
Culture Documents
Array
Array
Array
Arrays are data structures used to store multiple values in a single
variable, instead of declaring separate variables for each value.
For instance,
Instead of declaring individual variables, such as num0, num1, ..., and
num99, you declare one array variable such as nums and use nums[0], nums[1],
and ..., nums[99] to represent individual variables. A specific element in an
array is accessed by an index.
All arrays consist of contiguous memory locations. The lowest address
corresponds to the first element and the highest address to the last element.
Declaring arrays
To declare an array in C#, we can follow the following syntax -
Syntax – datatype[] name;
Examples – string[] employeeNames;
int[] numberOfTimes;
Initializing arrays
Declaring an array does not initialize the array in the memory. When the
array variable is initialized, you can assign values to the array.
Array is a reference type, so you need to use the new keyword to create an
instance of the array.
Example – string[] employeeNames = new string[26];
Assigning values to arrays
assign values to individual array elements, by using the
index number
string[] employeeNames = new string[26];
employeeNames[2] = “Mahi”;
assign values at the time of declaration
string[] employeeNames = {"Virat","Smriti","Mahi"};
create and initialize an array
string[] employeeNames = new string[3]
{"Virat","Smriti","Mahi"};
we can also omit the size of the array given
string[] employeeNames = new string[]
{"Virat","Smriti","Mahi"};
we copy an array variable into another target array variable.
In such case, both the target and source point to the same
memory location
string[] employeeNames = new string[]
{"Virat","Smriti","Mahi"};
string [] names = employeeNames;
Accessing array elements
An element is accessed by indexing the array name. This is done by
placing the index of the element within square brackets after the name of the
array.
Example – string[] employeeNames = new string[3]
{"Virat","Smriti","Mahi"};
string managerName = employeeNames[2];
Changing array elements
To change the value of a specific element, refer to the index number
Example – string[] employeeNames = new string[3]
{"Virat","Smriti","Mahi"};
employeeNames[1] = "Mithali";
Array length
To find out how many elements an array has, we can use the
Length property
Example – using System;
namespace Application{
class Program{
static void Main(string[] args){
string[] employeeNames = {"Virat","Smriti","Mahi"};
Console.Write(employeeNames.Length); //output is 3
}
}
}
Loops in arrays
We can loop through the array elements with the for loop, and use
the Length property to specify how many times the loop should run.
Example –
For loop -
using System;
namespace Application{
class Program{
static void Main(string[] args){
string[] employeeNames = {"Virat","Smriti","Mahi"};
for ( int i = 0; i < employeeNames.length; i++) {
Console.WriteLine(employeeNames[i]);
}}}}
For each loop –
The same program can be written using for each loop as well.
using System;
namespace Application{
class Program{
static void Main(string[] args){
string[] employeeNames = {"Virat","Smriti","Mahi"};
foreach ( string i in employeeNames )
{
Console.WriteLine(i);
}
Sorting arrays
There are many array methods available, for example Sort(), which sorts
an array alphabetically or in an ascending order
Example –
using System;
namespace Application{
class Program{
static void Main(string[] args){
string[] employeeNames = {"Virat","Smriti","Mahi"};
Array.Sort(employeeNames);
foreach ( string i in employeeNames )
{
Console.WriteLine(i);
}
}
}
}
System.Linq Namespace
Other useful array methods, such as Min, Max, and Sum, can be found in
the System.Linq namespace
Example –
using System;
using System.Linq;
namespace Application{
class Program{
static void Main(string[] args){
int [] nums = {3,6,2,8};
Console.WriteLine(nums.Max());
Console.WriteLine(nums.Min());
Console.WriteLine(nums.Sum());
}
}
}
Multi-dimensional arrays
Two dimensional arrays
The simplest form of the multidimensional array is the 2-dimensional
array. A 2-dimensional array is a list of one-dimensional arrays.
Here, this 2D array looking like table contains x rows and y columns and x * y
is the size of the array
Initializing 2D array
Multidimensional arrays may be initialized by specifying bracketed
values for each row.
int [ , ] arr = new int [3,5]{
{0,1,2,3,4}, //1st row with index 0
{5,6,7,8,9}, // 2nd row with index 1
{10,11,12,13,14} //3rd row with index 2
};