Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

CSharp.NET Tutorial – Day 9


In previous article, what are conditional Statements and types, what is Conditional Branching, what is Conditional Looping, what is ‘if’ statement, what is switch case, what is for loop, what is while loop, what is do while loop, what is foreach loop along with syntaxes and example programs, what is difference between array and collection, what are Jump Statements available in C# and syntaxes of goto, break, continue, and return statements along with some examples, etc

Please find below the link for accessing the article
CSharp.NET Tutorial - Day 8 

Now, in this article we will focus completely on concept of Arrays like what is an array, what are different types of arrays available like what is Single Dimensional, what is two Dimensional, what is jagged Array along with their syntaxes and some examples, what are Array class methods available like Sort, Reverse, Copy, GetLength, and Length, what is Foreach Loop, what is the difference between for and foreach loop, and what are the different ways of inputting values to a program like through ReadLine method of Console class and through CommandLine Parameters, etc

Array

An array is a set of similar type values.  We can store the values in three types of arrangements which are as follows.
1)      Single Dimensional
2)      Two Dimensional
3)      Jagged Array

In C#, array can be declared as fixed length or dynamic.  Fixed Length array can store a predefined number of items, whereas size of dynamic array increases based on the items we added to the array.

1) Single Dimensional Array
These arrays store the values in the form of a “Row.”





Syntax:
<type>[ ] <name> = new <type> [size];
int[ ] arr = new int[4];
or
int[ ] arr;
arr= new int[5];

or

int [ ] arr={list of values};
Eg:
using System;
class SDArray
{
static void Main()
{
int[] arr = new int[6];
for (int i = 0; i <= 6; i++)
Console.Write(arr[i] + " ");
Console.WriteLine();
arr[0] = 0; arr[1] = 20; arr[2] = 30;
arr[3] = 40; arr[4] = 50; arr[5] = 60;
foreach (int i in arr)
Console.Write(i + " ");
}
}

Foreach Loop:
In this foreach loop for each iteration of the loop, one value of the array is returned to us in a sequential order while processing the value of an Array or Collection.



Difference between ‘for’ and ‘foreach’
In for loop, the variable of the loop refers to the index of our array, whereas in foreach loop the variable of the loop refers to values of the array.

In for loop, the variable should always be ‘int’ only, whereas in foreach loop the loop variable type will be same as the type of values in the Array.

Note:  An array is a reference type because it gives us the option to initialize an array after declaration also.

Array Class
Under the System namespace, we are provided with a class known as Array and it provides us a set of methods and properties which can be used for manipulating of an array.  The below are some methods available in Array class.

-          Sort (array)
-          Reverse (array)
-          Copy (source, destination, n)
-          GetLength (index)
-          Length

Eg:
using System;
class SDArray2
{
static void Main()
{
int[ ] arr={34,90,29,15,62,78,32,4,72,94,89,5,16}
for(int i=0;i<arr.Length;i++)
Console.Write (arr[i] +" ");
Console.WriteLine ();
Array.sort (arr);
foreach (int i in arr)
Console.Write (i+" ");
Console.WriteLine ( );
Array.Reverse (arr);
foreach (int i in arr)
Console.Write (i+" ");
Console.WriteLine ();
int [ ] brr=new int[10];
Array.Copy (arr,brr,5);
foreach (int i in brr)
Console.Write (i+" ");
}
}

Taking input into a program
There are two different ways that we can take input into a program.
1)      Using ‘ReadLine’ method of Console class
2)      Using ‘CommandLine’ Parameters

In the second case, while executing a program from command prompt, we can supply values to the program from Command Prompt, where all the values we supply will be taken into the “String Array” of Main method.

Write the following program in order to check the process

using System;
class ParasEx
{
static void Main(string[] args)
{
foreach (string str in args)
Console.WriteLine(str);
}
}

After compiling the above program, execute program from command prompt and check the result.

Eg:
C:\CSharp>ParasEx 10 Hello True 3.14

Eg:
using System;
class AddParams
{
static void Main(string[] args)
{
int sum=0;
foreach (string str in args)
sum + = int.Parse(str);
Console.WriteLine (sum);
}
}

2) Two Dimensional Arrays
The two dimensional arrays store the data in the form of Rows and Columns.


Syntax:
<type>[,] <name> = new<type>[rows, cols];
int[,] arr = new int[3,4];

or

int[,] arr;
arr = new int[2,3];
int[,] arr = (list of values);

Eg:
using System;
class TDArray
{
static void Main( )
{
int[,] arr = new arr[4,5];
int x=5;

//Assigning values to 2DArray:
for(int i=0;i<arr. GetLength(0);i++)
{
for(int j=0;j<arr. GetLength(1);j++)
{
arr[i,j]=x;
x + =5;
}
}

//Printing values of 2DArray:
for(int i=0;i<arr. GetLength(0);i++)
{
for(int j=0;j<arr. GetLength(1);J++)
Console.Write (arr [i, j] +" ");
Console.WriteLine ();
}
}
}

Assigning values to 2DArray at the time of execution
int [,] arr =
{
{11, 12, 13, 14},
{21, 22, 23, 24},
{19, 20, 21, 22},
};

Jagged Arrays
Jagged arrays are also similar to Two Dimensional arrays like it contains data in the form of Rows and Columns, but the difference is the number of columns of each row will be varying.

These are also called as Array of Arrays because multiple single dimensional arrays are combined together to form a new Array.


Syntax:
<type>[][]<name> = new <type>[rows][];
int[][] arr = new[3][];

or

int[][] arr = {list of values};

Please note that in the initial declaration of a jagged array, we can also specify the number of rows to the array, but not columns because the column size is not fixed.

After specifying the number of rows to the Array pointing to each row, we need to specify the number of columns we want for that Row.

Eg:
int [][] arr = new int[4][];
arr [0] = new int[5];
arr [1] = new int[6];
arr [2] = new int[8];
arr [3] = new int[4];


Eg:
using System;
class JaggedEx
{
static void Main()
{
int [][] arr = new int[4][];
arr [0] = new int[5];
arr [1] = new int[6];
arr [2] = new int[8];
arr [3] = new int[4];

//Assigning values to jagged array:
for(int i=0;i<arr[i].Length;i++)
{
for(int j=0;j<arr[i].Length;j++)
arr[i][j]=j+1;
}

//Printing values of jagged array:
for(int i=0;i<arr. GetLength(0);i++)
{
for(int j=0;j<arr[i].Length;j++)
Console.Write (arr[i][j]+" ");
Console.WriteLine ();
}
}
}

We can also print values of a jagged array like below

for(int i=0;i<arr. GetLength(0);i++)
{
foreach(int x in arr[i])
Console.Write (x+" ");
Console.WriteLine ();
}

Assigning values to jagged array at the time of execution

int[][] arr = {
new int[3] {11,12,13}
new int[5] {21,22,23,24,25}
new int[4] {31,32,33,34}
new int[2] {41,42}
}

Happy Learning ...... !!!!!!  

CSharp.NET Tutorial - Day 10


This post first appeared on Dot Net Programming (C#, Asp.Net, ADO.Net, WCF, WPF, Ajax, LINQ), please read the originial post: here

Share the post

CSharp.NET Tutorial – Day 9

×

Subscribe to Dot Net Programming (c#, Asp.net, Ado.net, Wcf, Wpf, Ajax, Linq)

Get updates delivered right to your inbox!

Thank you for your subscription

×