Object Oriented Programming
CSC2071
Lecture No. 06
Muhammad Shahid
Department of Computer Science
National Textile University
shahid.abdullah@hotmail.com
Recap
What are Methods?
Structure of a Method
Methods and Parameter Modifiers
‒ The out Modifier
‒ The ref Modifier
‒ The params Modifier
Method Overloading
2 Object Oriented Programming – CSC2071
What Will You Learn Today?
Arrays
Multidimensional Arrays
‒ Rectangular arrays
‒ Jagged arrays
Arrays as Arguments
Arrays as Return values
The System.Array Base Class
3 Object Oriented Programming – CSC2071
Arrays
4 Object Oriented Programming – CSC2071
Arrays
string[] str = new string[]{"One","Two","Three"};
string[] str = {"One", "Two", "Three"};
int[] intArray = new int[4]{20, 22, 23, 0};
int[] dblArray = {10.01,20.02,30.03,40.04,50.05};
5 Object Oriented Programming – CSC2071
Arrays
static void Main(string[] args)
{
const int SIZE = 10;
int [] myArrays = new int[SIZE];
for(int i=0; i<SIZE; i++)
{
Console.Write("Element:\t{0}", i);
}
}
6 Object Oriented Programming – CSC2071
Multidimensional Arrays
C# supports two varieties of multidimensional
arrays
‒ Rectangular arrays
‒ Jagged arrays
Rectangular arrays are simply arrays of
multiple dimensions, where each row is of the
same length
7 Object Oriented Programming – CSC2071
Rectangular Arrays
int[,] rectangularArrays = new int[2,2];
int[ , ] recatngularArray = {{1,2}, {3,4}};
Column 0 Column 1 Column 2 Column 3
Row 0 array[0,0] array[0,1] array[0,2] array[0,3]
Row 1 array[1,0] array[1,1] array[1,2] array[1,3]
Row 2 array[2,0] array[2,1] array[2,2] array[2,3]
8 Object Oriented Programming – CSC2071
Rectangular Arrays
int[,] arr = new int[3,3];
// Outer loop for rows
for(int i=0; i<3; i++)
{
// Inner loop for columns
for(int j=0; j<3; j++)
{
Console.Write("Enter Element");
arr[i,j]= int.Parse(Console.ReadLine());
}
}
9 Object Oriented Programming – CSC2071
Rectangular Arrays
Console.WriteLine("------------------");
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
Console.Write(arr[i,j], "\t");
}
Console.WriteLine();
}
Console.WriteLine("------------------");
10 Object Oriented Programming – CSC2071
Rectangular Arrays
Enter Array Element 1
Enter Array Element 2
Enter Array Element 3
Enter Array Element 4
Enter Array Element 5
Enter Array Element 6
Enter Array Element 7
Enter Array Element 8
Enter Array Element 9
-------------------------------
1 2 3
4 5 6
7 8 9
-------------------------------
11 Object Oriented Programming – CSC2071
Jagged Arrays
Jagged arrays contain some number of inner
arrays, each of which may have a different
upper limit
A jagged array is maintained as a one-
dimensional array in which each element
refers to a one-dimensional array
The manner in which jagged arrays are
represented makes them quite flexible,
because the lengths of the rows in the array
need not be the same
12 Object Oriented Programming – CSC2071
Jagged Arrays
int[][] jagged = { new int[] {1, 2},
new int[] {3},
new int[] {4, 5, 6}
};
13 Object Oriented Programming – CSC2071
Jagged Arrays
static void Main(string[] args)
{
int[][] jagged = new int[3][]; int i;
jagged[0] = new int[4];
jagged[1] = new int[3];
jagged[2] = new int[5];
// Store values in first array
for(i=0; i<4; i++) { jagged[0][i] = i; }
// Store values in second array
for(i=0; i<3; i++) { jagged[1][i] = i; }
// Store values in third array
for(i=0; i<5; i++) { jagged[2][i] = i; }
14 Object Oriented Programming – CSC2071
Jagged Arrays
// Display values in first array
for(i=0; i<4; i++)
Console.Write(jagged[0][i] + "\t");
// Display values in second array
for(i=0; i<3; i++)
Console.Write(jagged[1][i] + "\t");
// Display values in third array
for(i=0; i<5; i++)
Console.Write(jagged[2][i] + "\t");
}
15 Object Oriented Programming – CSC2071
Jagged Arrays
0 1 2 3
0 1 2
0 1 2 3 4
16 Object Oriented Programming – CSC2071
Arrays as Arguments & Return Values
static void Display(int[] array)
{
for(int i=0; i<array.Length; i++)
Console.WriteLine("{0}", array[i]);
}
static string[] GetStrings()
{
string[] str = { "CS", "NTU", "FSD" };
return str;
}
17 Object Oriented Programming – CSC2071
Arrays as Arguments or Return Values
static void Main(string[] args)
{
// Pass array as parameter
int[] ages = {20, 24, 23, 22} ;
Display(ages);
// Get array as return value
string[] strs = GetStrings();
foreach(string str in strs)
{
Console.WriteLine(str);
}
}
18 Object Oriented Programming – CSC2071
The System.Array Base Class
Members Meaning
Clear() This static method sets a range of elements in the array
to empty values (0 for numbers, null for object references,
false for Booleans)
CopyTo() This method is used to copy elements from the source
array into the destination array
Length This property returns the number of items within the array
Rank This property returns the number of dimensions of the
current array
Reverse() This static method reverses the contents of a one-
dimensional array
Sort() This static method sorts a one-dimensional array of
intrinsic types
19 Object Oriented Programming – CSC2071
Recap
Arrays
Multidimensional Arrays
‒ Rectangular arrays
‒ Jagged arrays
Arrays as Arguments
Arrays as Return values
The System.Array Base Class
20 Object Oriented Programming – CSC2071
Questions
Object Oriented Programming – CSC2071