Introduction
This
article explain about arrays, how to declare and use in c#.net
Contents
- Definition
- Types of Arrays
- Passing array as parameter
- foreach on Array
Definition:
Array
is a group of elements of same type. Array is data structure that contains a
number of variables called elements of array. Arrays is zero based indexed i.e.
index starts from zero. Array can be of any type. Arrays are reference types
Types
of Arrays
1.
Single –
Dimensional Arrays
2.
Multi – Dimensional
Arrays
3.
Jagged Arrays
1.
Single
– Dimensional Array
Array Declaration
Syntax:
<datatype>[] <arrayname>=new
<datatype>[arraysize];
int[] a=new int[12];
string[] s=new string[10];
Array Initialization
int[] n=new int[3]{1,2,3}; -- Array initialize by mentioning size
int[] n=new int[]{1,2,3}; -- Array initialization without mentioning
size
string[] months=new
string[] {“Jan”,”Feb”,”Mar”,”Aprl”,”May”,”Jun”,”July”,”Aug”,”Sep”,”Oct”,”Nov”,”Dec”};
Assigning Value to Arrays
n[0]=23;
months[0]=”January”;
2.
Multi
– Dimensional Array
Array Declaration
Syntax:
int[,]
n=new int[3,4]; -----> 3 rows and 4 columns
int[,,]
n=new int[2,3,4] ----->Three dimensional array
Array
Initialization
int[,]
n=new int[2,3]{ {2,3,4}, {45,67,8} };
int[,]
n=new int[,]{ {2,3,4}, {45,67,8} };
string[,]
s=new string[,]{ {“abc”,”xyz”},
{“pqr”,”lmn”} };
Assigning
Value to Arrays
n[0,0]=1;
s[0,0]=”sateesh”;
3.
Jagged
Array
A jagged array is an array whose elements are
arrays. The elements of a jagged array can be of different dimensions and
sizes.
Array
Declaration
Example:
Following is array of array. It
has 3 rows, 4 elements in first row, 2 elements in second row, 5 elements in
third row, 3 elements in 4th row
1
|
2
|
3
|
4
|
|
34
|
23
|
|||
12
|
13
|
14
|
151
|
16
|
7
|
8
|
9
|
int[][]
n=new int[4][];
n[0]=new int[4];
n[1]=new int[2];
n[2]=new int[5];
n[3]=new int[3];
Array
Initialization
int[][] n=new int[][]{
new
int[]{1,2,3,4},
new
int[]{34,23},
new
int[]{12,13,14,151,16},
new
int[]{7,8,9}
};
Or
n[0]= new int[]{1,2,3,4};
n[1]= new int[]{34,23};
n[2]= new int[]{12,13,14,151,16};
n[3]= new int[]{7,8,9};
or
int[][]
n={
new
int[]{1,2,3,4},
new
int[]{34,23},
new
int[]{12,13,14,151,16},
new
int[]{7,8,9}
};
Assigning
Value to Arrays
n[0][0]=1;
n[0][1]=2;
……
n[1][0]=34;
Passing
Array to Parameters
class Program
{
static void Main(string[] args)
{
int[] a = new int[] { 1, 2, 3, 4 };
test(a);
for (int i = 0; i < a.Length; i++)
Console.WriteLine(a[i]);
}
static void test(int[] n)
{
for (int i = 0; i < n.Length; i++)
Console.WriteLine(n[i]);
n[2]=23;
}
}
Output:
1
2
3
4
1
23
3
4
Array ‘a’
is passed to method test as reference. Hence elements of array ‘a’ is accessed
and printed on console from test method. And also update the 2nd element of
array ‘a’ is updated to 23.
foreach
on Array
foreach can be used with all types of arrays
class Program
{
static void Main(string[] args)
{
int[] a = new int[] { 1, 2, 3, 4 };
test(a);
for (int i = 0; i < a.Length; i++)
Console.WriteLine(a[i]);
}
static void test(int[] n)
{
foreach (int i in n)
Console.WriteLine(i);
n[2]=23;
}
}
Output:
1
2
3
4
1
23
3
4
class Program
{
static void Main(string[] args)
{
int[,] a = new int[,] { {1, 2, 3, 4},{7,8,9,10} };
test(a);
}
static void test(int[,] n)
{
foreach (int i in n)
Console.WriteLine(i);
n[1,1]=23;
}
}
Output:
1
2
3
4
7
8
9
10