C# .Net Array PDF
C# .Net Array PDF
An array is a list of items or collection of elements which is of same or similar data type.
An array is a collection of elements of a single data type stored in adjacent memory
locations.
An array is a collection of related values placed in contiguous memory locations and these
values are referenced using a common array name.
An array simplifies the task of maintaining these values
An array always stores values of a single data type.
Each value is referred to as an element.
These elements are accessed using subscripts or index numbers that determine the
position of the element in the array list.
C# supports zero-based index values in an array.
This means that the first array element has an index number zero while the last element
has an index number n-1, where n stands for the total number of elements in the array.
This arrangement of storing values helps in efficient storage of data, easy sorting of data,
and easy tracking of the data length.
Declaring Arrays
Declaration
An array declaration specifies the type of data that it can hold and an identifier.
This identifier is basically an array name and is used with a subscript / index to retrieve or
set the data value at that location.
Memory allocation:
Declaring an array does not allocate memory to the array.
Following is the syntax for declaring an array:
data_type[] arrayName;
data_type: Specifies the data type of the array elements (for example, int and char).
arrayName: Specifies the name of the array.
Initializing Arrays
The following table lists the default values for some of the widely
used data types:
int 0
float 0.0
double 0.0
char '\0'
string Null
Size-value: Specifies the number of elements in the array. You can specify a variable of
type int that stores the size of the array instead of directly specifying a value.
Example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ArraysDemo
{
class Program
{
static void Main(string[] args)
{
string[] myArray = { "ANKIT", "SAINI", "AMAN", "RAM" };
Console.WriteLine(myArray.Length);
for (int i = 0; i < myArray.Length; i++)
{
Console.WriteLine(myArray[i]);
}
foreach (string name in myArray)
{
Console.WriteLine(name);
}
Console.WriteLine("Foreach Loop Ends..");
int[] myArray = new int[] { 10,20,30,40 };
int[] myArray = new int[4];
myArray[0] = 10;
myArray[1] = 20;
myArray[2] = 30;
myArray[3] = 40;
Console.WriteLine(myArray[0]);
Console.WriteLine(myArray[1]);
Console.WriteLine(myArray[2]);
Console.WriteLine(myArray[3]);
Console.ReadLine();
}
}
}
int[ , ] x = { { 1, 2 ,3}, { 3, 4, 5 } };
X is a multidimensional array which has two elements: {1, 2, 3} and {3, 4, 5}. And,
each element of the array is also an array with 3 elements.
Two-dimensional array in C#
A two-dimensional array consists of single-dimensional arrays as its elements. It can
be represented as a table with a specific number of rows and columns.
int[ , ] x = { { 1, 2 ,3}, { 3, 4, 5 } };
X is a 2D array with two elements {1, 2, 3} and {3, 4, 5} . We can see that each
element of the array is also an array.
We can also specify the number of rows and columns during the initialization. For
example,
// a 2D array
int[ , ] x = { { 1, 2 ,3}, { 3, 4, 5 } };
Example: C# 2D Array
using System;
namespace MultiDArray {
class Program {
static void Main(string[] args) {
//initializing 2D array
int[ , ] numbers = {{2, 3}, {4, 5}};
// access first element from the first row
Console.WriteLine("Element at index [0, 0] : "+numbers[0, 0]);
// access first element from second row
Console.WriteLine("Element at index [1, 0] : "+numbers[1, 0]);
}
}
}
numbers[0, 0] - access the first element from the first row (2)
numbers[1, 0] - access the first element from the second row (4)
using System;
namespace MultiDArray {
class Program {
static void Main(string[] args) {
// old element
Console.WriteLine("Old element at index [0, 0] : "+numbers[0, 0]);
// new element
Console.WriteLine("New element at index [0, 0] : "+numbers[0, 0]);
}
}
}
Iterating C# Array using Loop
using System;
namespace MultiDArray {
class Program {
static void Main(string[] args) {
}
Console.WriteLine();
}
}
}
}
In the above example, we have used a nested for loop to iterate through the elements
of a 2D array.
numbers.GetLength(0) - gives the number of rows in a 2D array
numbers.GetLength(1) - gives the number of elements in the row
Note: We can also create a 3D array. Technically, a 3D array is an array that has
multiple two-dimensional arrays as its elements. For example,
int[ , , ] numbers = { { { 1, 3, 5 }, { 2, 4, 6 } },
{ { 2, 4, 9 }, { 5, 7, 11 } } };