You are on page 1of 8

C# BASICS

By,manish pushkar
Quaarc Solutions


1
Arrays in C#
An Array is a kind of variable which can hold multiple variable for same data
type.Whenever we create an array then it is automatically inherit from System.Array
class.
This class defines a number of methods and properties that can be used
to manipulate array more efficiently,which are given below:-
Method/Property Meaning
Set value() Sets the value for a given index in the array
Reverse() Reverse the contents of a one -dimension array
sort() Sorts the elements in a one -dimensional array
Get Value() Get the value for a given index in the array
Get Length() Gives the number of elements in a given dimension of
the array
Clear() Sets a range of elements to empty values
Copy To() Copies elements from the source array to the
destination array
Length() Gives the Length of an array


Note:-Array List Class i have already discussed in Collections.So there is no need to
explain again.
Creation of an array involves three steps.
1. Declaring the array (Declaration of Arrays).
2. Creating memory locations (Creation of Arrays).
3. Initialization of Arrays.
1 . ) Declaration of Arrays:-
Arrays are declared in C# as follows:-
Syntax:
type [ ] arrayname ;
Ex.
int [ ] num; //declaration int array reference
float [ ] price ; // declaration float array refernce
Note:- We do not enter the size of the arrays in the declaration.
2 . ) Creation of Arrays:
We need to create it in the memory after declaration of an array.In C# ,we use
a 'new' operator for creating an Array.
Syntax:
arrayname = new type [ size];
Ex.
num = new int [ 10]; //Create a 10 element int array
price = new float [20 ]; //create a 20 element float array.
C# BASICS
By,manish pushkar
Quaarc Solutions


2
Note:- we can combine both steps (Declaration and creation of an array) into one.
Ex.
int [ ] num = new int [ 10];
float [ ] price = new int [20];
3 . ) Initialization of arrays:-
To put the values in arrays is known as initialization.There are some different Example
to initialization values in the arrays.
Syntax:
arrayname[ subscript] = value;
Ex 1:
num [ 0] = 20 ;
num [ 1 ] = 30 ;
.
.
num [n ] = n ;
Note:- In C# , An Array start with a subscript of 0 (zero) and ends with a value less
than the size of the array.
Automatic initialization of the array at creation time.
Syntax:-
type [ ] arrayname = {list of values with commas } ;
Ex. 2
int [ ] num = { 20,40,50,30 };
We can easily combine all three steps (Declaration,creation,initialization) in one.
Syntax:
int [ ] num = new int [ size] {list of values with commas };
Ex. 3
int [ ] num = new int [ 10] {20,30,50,40 };
Note:-
Every element in a array is automatically set to a default value.For example if we have an
array of numeric type ,each element is set to number 0(zero).
Types of Arrays
There are two types of Arrays .
1. Single dimensional arrays
2. Multi dimensional arrays
1. ) Single dimensional arrays:-
A list of items that can be given one variable name using one subscript,such a variable is
called a single dimensional array. It is also called linear array.
C# BASICS
By,manish pushkar
Quaarc Solutions


3


Ex.
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
using System;
namespace Single_dimensional
{
class Program
{
static void Main(string[] args)
{
//declaration,creation and initialization in one step.
string[] str = new string[4]{"Ram","Mohan","Ajay","Sanjay"};
for (int i = 0; i < 4; i++)
{
Console.WriteLine(str[i]);
}
Console.ReadLine();
}
}
}



C# BASICS
By,manish pushkar
Quaarc Solutions


4
Output:


2. ) Multi dimensional array:-
An array variable that can store the table values, is known as multi dimensional array.It
can categories into two types.
Rectangle array
Jagged array
Rectangle array:-
An Array , each row has same size of column, is known as Rectangle array.

Ex.
?
1
2
3
4
5
using System;
namespace arrays
{
class Program
{
C# BASICS
By,manish pushkar
Quaarc Solutions


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
static void Main(string[] args)
{
Console.WriteLine("Enter the Row size");
int row = int.Parse(Console.ReadLine());
Console.WriteLine("Enter the Column size size");
int col = int.Parse(Console.ReadLine());
int [,] str = new int [row, col];
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 2; j++)
{
Console.WriteLine("Enter matrix value
({0},{1})",i,j);
str[i, j] = int.Parse(Console.ReadLine());
}
}
Console.WriteLine("\n");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 2; j++)
{
Console.WriteLine("Rectangle arrays values({0},{1})
",i,j);
Console.WriteLine(str[i,j]);
}
}
Console.ReadLine();
}
C# BASICS
By,manish pushkar
Quaarc Solutions


6
31
32
33
}
}
Output:


Jagged Array:-
Jagged array is an array of array,In which each row has different size of column.





C# BASICS
By,manish pushkar
Quaarc Solutions


7
Ex.
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
using System;
namespace jagged_array
{
class Program
{
static void Main(string[] args)
{
int[][] jarr = new int[3][];
jarr[0] = new int[1];
jarr[1] = new int[2];
jarr[2] = new int[3];
jarr[0][0]=34;
jarr[1][0]=12;
jarr[1][1]=38;
jarr[2][0]=65;
jarr[2][1]=50;
jarr[2][2]=43;
for(int i=0;i<1;i++)
{
Console.WriteLine("jagged array 0
elements:[0][{0}]={1}",i,jarr[0][i]);
}
for (int i =0; i < 2; i++)
{
Console.WriteLine("jagged array 1 elements:[1][{0}]={1}", i,
C# BASICS
By,manish pushkar
Quaarc Solutions


8
24
25
26
27
28
29
30
31
32
33
jarr[1][i]);
}
for (int i = 0; i < 3; i++)
{
Console.WriteLine("jagged array 2 elements:[2][{0}]={1}", i,
jarr[2][i]);
}
Console.ReadLine();
}
}
}
Output:

You might also like