• USA : +1 973 910 5725
  • INDIA: +91 905 291 3388
  • info@tekslate.com
  • Login

Arrays in C# .Net

Arrays

Arrays are of type “reference”

That is even if an Array is not initialized still it holds some default values.

int a, b, c ;

C = a + b;

STACK

Screenshot_23

Array 

X(5)       x=5000

HEAP

 

Screenshot_24

 

Array is a collection of homogeneous data type

Arrays shares same name with different index values

Always index starts from ‘0’

Arrays will be stored in continuous memory

Arrays are faster in accessing

All the arrays are instance of system. Array class

                     ↓

C# supports three types of Arrays

One dimensional arrays

Multi dimensional arrays

Jagged Arrays

 

Screenshot_25

 

Screenshot_26

 

Working with one dimensional array

Syntax

Data type []  arr_name  = new data type [size];

Example

Screenshot_27

 

While initializing an array , size is not recommended

Example

A program to print array default rules:-

Open window forms application project

Place a button

Code for button 1 click

{

Int [] x = new int [2];

For  (int  I =0 ; i< arg.length ; I ++);

Message Box. Show ( x [i] + “”);

}

Output

 

Screenshot_29

Observations

Integral Array default value is Zero.

Floating Array default value is Zero.

char Array default value is nothing.

Bool Array default value is false.

Date time Array default value is 1/1/0001 12:00:00 AM.

Example

A program to create or get the spelling of a given number     (1…………………..999)

123…………..one hundred twenty three

Open windows forms application project

Place two text boxes and a button

Screenshot_30

 

Code for button1 click

{

Int n = int. parse (text box 1.text);

Int I = 0;

String res = “”;

String [] ones  =  new string []  { “one”, “two”, “three”, “four” “five”, “six” ………………,” nineteen”};

String [] Tens = new string [] {“ten”, “twenty”, “ninety”};

N = 426

N = 426/ 100  -à 10/3 = 3

 

Screenshot_31

 

If (n > 99 && n < 1000)

{

I = n / 100;

Res = ones [I – 1] + “hundred”;

N = n % 100;

}

If (n > 19 && n < 1000)

{

I = n / 100;

Res = ones [I – 1] + “hundred”;

N = n % 100;

}

If (n > 0&& n < 20)

Res =res +  ones [I – 1] ;

Text Box 2.text = res;
F5}

Screenshot_32

Working with Multi Dimensional Arrays

Arranging a set of elements in rows and columns is called  “Multi Dimensional Array”.

In Multi Dimensional Array , every row must contain same number of elements.

Multi Dimensional Array size will be represented  with no. of rows X  no. of columns

If the array name is ‘X’ then x. length return total number of elements.

 

Screenshot_33

get length (0) returns no. of rows

get length (1) returns no. of columns

syntax to declare multi – d array

I0 : int [] x = new int [s];

S arrrow

20 : int [,] x = new int [s,s];

30 : int [,,] x = new int [s,s,s];

Note

VB supports up to 32 dimensions where as no limit in C #.NET

Syntax to initialize 2 D Array

 

Int [,] x = new int [,]

{

{5,6,7,8}

{9,8,7,6}

{5,5,5,5}

}

 

Screenshot_34

 

Example with multi dimension Array

Open windows forms application project

Place a button

Code for button 1 click

 

{

String  s = “”;

Int [,]  x = new int [,];

{

{5, 6, 7, 8},

{9, 8, 7, 6},

{5, 5, 5, 5}

};

 

r=0                  c = 0

Rr=1                    c =1

c =2

c =3

message  box. Show (“Total :” + x. length) ;

message box .show  (“Rows : + x. get length  (0)”)

+ “ cols :”  + x. get length  (1) );

For (int  r = 0; r < x. get length  (0);r ++)

{

For (int  c = 0; c< x. get length  (0);c ++)

{

S = S + X [r,c] + “” ;

}

S= s + “/n”

}

Message Box. Show (s);

Jagged Arrays

ABC(Company)

Members are working

Employees   arrrow3*6arrrowvalues

Screenshot_35

 

Jagged array is a collection of rows where every row may contain different number of elements.

Jagged arrays save memory.

Jagged arrays are faster in accessing.

Jagged arrays is also called as “Dynamic array” (run time array)

Jagged array is also called as array of arrays.

Syntax

Int []  [] x = new int [3] [];

X [0] = new int [] {4,2,3,5,6,7}

X [1] = new int [] {3,8,4,2,4}

Example

Open windows forms application project

Place a button

Code for button 1 click

 

{

Int [] []   x = new int [3][];

X [0] = new int [] {5,3,4,6,7,7};

X [1] = new int [10];

X [2] = new int []{2,3};

String  S = “”;

For  (int r  = 0;r < x.get length (0);r ++)

{

For  (int c  = 0;r < x.get length (0);c++)

{

S= s + x[ r][c] +””;

}

S =s + “/n”

}

Message Box. Show (s);

 

“At TekSlate, we are trying to create high quality tutorials and articles, if you think any information is incorrect or want to add anything to the article, please feel free to get in touch with us at info@tekslate.com, we will update the article in 24 hours.”

0 Responses on Arrays in C# .Net"

Leave a Message

Your email address will not be published. Required fields are marked *

Site Disclaimer, Copyright © 2016 - All Rights Reserved.