Presentation is loading. Please wait.

Presentation is loading. Please wait.

When an argument to method is an entire array or an individual element of reference type, the called method receives a copy of reference. However an argument.

Similar presentations


Presentation on theme: "When an argument to method is an entire array or an individual element of reference type, the called method receives a copy of reference. However an argument."— Presentation transcript:

1 When an argument to method is an entire array or an individual element of reference type, the called method receives a copy of reference. However an argument to method is an individual array of value type, the called method receives a copy of the elements value. If you want to pass a value type array element to method by reference you must use the ref keyword.

2 namespace reffoouut { class array int[] a; public array() a = new int[] { 0, 0, 0, 0 }; } public void seq(int[] m) for (int i = 0; i < m.Length; i++) m[i] *= 2; public void trip( int x) x*=3; static void Main(string[] args) int [] d=new int [] {2,2,2}; array ar=new array(); ar.seq (d); ar.trip ( d[1]); foreach (int x in d ) Console.WriteLine(x); } } }

3 ~ classname ( ) { // statement ; } Garbage Collection & Destructors:

4 Two-Dimensional array:
Rectangular array int [ , ] mat =new int [3,3]; int [ , ] mat =new int [ , ]{{0,0,0},{1,1,1},{3,3,3}}; for (i=0; i<3; i++) for (j=0; j<3; j++) console .writeline(mat [i][j]);

5 Methods of array No.of Dimension : Rank
total No.of elements in the array : Length No.of D1 (Rows ) : GetLength(0) No.of D2(Column) : GetLength(1) No.of D : GetLength(2) lowerbound of D1 (Rows ) : GetLowerBound(0) lowerbound of D2(Column) : etLowerBound(1) upperBound of D1 (Rows ) : GetUpperBound(0) upperBound of D2(Column) : GetUpperBound(1)

6 using System; namespace array2D { class array int [,] x; public array(int n,int m) x = new int[n,m]; } public void Get_information() Console.WriteLine("No.of Dimension = {0}\n ", x.Rank); Console.Write("total No.of elements in the array ={0}\n\n", x.Length); Console.Write(" No.of Rows ={0}\n ", x.GetLength(0)); Console.WriteLine("No.of Column={0}\n", x.GetLength(1)); Console.Write("lowerbound of row: {0}\n", x.GetLowerBound(0)); Console.WriteLine("upperBound of row: {0}\n", x.GetUpperBound(0)); Console.Write("lowerbound of column: {0}\n", x.GetLowerBound(1)); Console.WriteLine("upperBound of column: {0}\n", x.GetUpperBound(1));

7 public void read_array()
{ for (int i = 0; i < =x.GetUpperBound(0); i++) for (int j = 0; j <= x.GetUpperBound(1);j++) Console.Write("Enter value at location {0},{1} In Array: ", i, j); x[i, j] = int.Parse(Console.ReadLine()); }

8 public void write_Matrix()
{ Console.WriteLine(); for (int i = 0; i <= x.GetUpperBound(0); i++) for (int j = 0; j < =x.GetUpperBound(1);j++) Console.Write("{0}\t",x[i, j]); }

9 static void Main(string[] args)
{ array a=new array(3,5); a.Get_ information (); a.read_array(); a.write_Matrix(); }

10 Jagged arrays int [] [] x; type [] []...
Access the fifth element of the third array by writing x[2][4].

11 Example creates a jagged array named x, initializes its elements, and then prints their content. To save space, the program takes advantage of the fact that integer array elements are automatically initialized to 0, and it initializes the values of only some of the elements. using System; using System.Collections.Generic; using System.Text; namespace JaggedArray { public class Tester

12 static void Main( ) { const int rows = 4; int[][] x = new int[rows][]; // declare the jagged array as 4 rows high x[0] = new int[5]; // the first row has 5 elements x[1] = new int[2]; // a row with 2 elements x[2] = new int[3]; // a row with 3 elements x[3] = new int[5]; // the last row has 5 elements // Fill some (but not all) elements of the rows x[0][3] = 15; x[1][1] = 12; x[2][1] = 9; x[2][2] = 99; x[3][0] = 10; x[3][1] = 11; x[3][2] = 12; x[3][3] = 13; x[3][4] = 14;

13 for ( int i = 0; i < 5; i++ )
{ Console.WriteLine( "x[0][{0}] = {1}",i, x[0][i] ); } for ( int i = 0; i < 2; i++ ) Console.WriteLine( "x[1][{0}] = {1}",i, x[1][i] ); for ( int i = 0; i < 3; i++ ) Console.WriteLine( "x[2][{0}] = {1}", i, x[2][i] ); Console.WriteLine( "x[3][{0}] = {1}", i, x[3][i] );

14 Output: x[0][0] = 0 x[0][1] = 0 x[0][2] = 0 x[0][3] = 15 x[0][4] = 0 x[1][0] = 0 x[1][1] = 12 x[2][0] = 0 x[2][1] = 9 x[2][2] = 99 x[3][0] = 10 x[3][1] = 11 x[3][2] = 12 x[3][3] = 13 x[3][4] = 14


Download ppt "When an argument to method is an entire array or an individual element of reference type, the called method receives a copy of reference. However an argument."

Similar presentations


Ads by Google