Arrays
• Array: A reference type or a value type?
• Reference types: Arrays, Classes, Delegates etc.
• Value types: All numeric data types, Boolean, char etc.
• Storage: Where arrays are stored? Stack or Heap?
• Reference variables are stored on stack, but the values
they reference are stored on heap.
• How toallocate memory on heap?
• Using new keyword.
• int[] a = new int[5] {3,15,0,-2,7};
• Stack: a
• Heap : 3,15,0,-2,7
Multidimensional Arrays(1/1)
• 1D int[], 2D int[,], 3D int[,,]
• Which of the following is wrong and why?
int[] array;
int array = {1,2,3,4};
array = {1,2,3,4};
• If you choose todeclare an array variable without initialization, you
must use the new operator toassign an array tothe variable.
int[] array;
array = new int[] {1,2,3,4}; int array = {1,2,3,4};
//need toassign array tovariable //Works fine
Multidimensional Arrays(1/2)
• TwoDimensional Arrays
• How toassign a value toan array?
• array2D [2,1] = 98
Multidimensional Arrays(2/2)
• Three dimensional Arrays
Problems with multidimensional Arrays
• Unfortunately, in the C# language, two-dimensional arrays and all multi-
dimensional arrays are slower than one-dimensional arrays when accessing
large number of elements.
• Solution: Jagged Arrays: storage and retrieval from jagged (or single
dimensional) arrays are simple
• What is Difference between Jagged Array and Multidimensional Array?
• A jagged array is an array-of-arrays, so an int[][] is an array of int[], each of
which can be of different lengths and occupy their own block in memory. A
multidimensional array (int[,]) is a single block of memory (essentially a
matrix).
Jagged Arrays
• A jagged array is an array whose elements are arrays.
• The elements of a jagged array can be of different dimensions and
sizes.
• A jagged array is an array whose elements are arrays.
• A jagged array is sometimes called an "array of arrays.“
int[][] jaggedArray = new int[3][];
jaggedArray[0] = new int[5];
jaggedArray[1] = new int[4];
jaggedArray[2] = new int[2];
Jagged Arrays
using System; class Program { static void Main() {
// Declare local jagged array with 3 rows.
int[][] jagged = new int[3][];
// Create a new array in the jagged array, and assign it.
jagged[0] = new int[2];
jagged[0][0] = 1;
jagged[0][1] = 2;
// Set second row, initialized tozero.
jagged[1] = new int[1]; // Set third row, using array initializer.
jagged[2] = new int[3] { 3, 4, 5 }; // Print out all elements in the jagged array
for (int i = 0; i < jagged.Length; i++)
{ int[] innerArray = jagged[i];
for (int a = 0; a < innerArray.Length; a++)
{ Console.Write(innerArray[a] + " ");
} Console.WriteLine(); } } }
Debugging Classes
• It's possible that you may attempt tostep intoclass code and fail
because of automatic step-over, and see this dialog box
• Toprevent automatic step-over, you can
either place a breakpoint within the
class element you want tostep into, or
you can change the StudioProperty that
controls this behavior in
• Debug | Options and Settings. Select the
Debugging in the options tree, and uncheck
Step over properties and operators
(Managed only)