Docsity

Arrays, ArrayLists, SortedLists, and Dictionaries in C#, Slides of C Sharp Programming

Partial preview of the text

Download Arrays, ArrayLists, SortedLists, and Dictionaries in C# and more Slides C Sharp Programming in PDF only on Docsity!

Working With C# Arrays

And .NET Lists

Lecture Overview

 Discuss arrays  Discuss the various types of lists available from C# and the .NET Framework

Arrays (Introduction 2)

 Arrays are reference types  Memory is allocated from the managed heap

 Uninitialized arrays will throw null reference exceptions

Test whether an array references null if (DemoArray1 == null) { // It’s a null pointer. }

Arrays (Introduction 3)

 Arrays (almost always) have a lower bound (smallest subscript) of 0  We can trick .NET to create non-zero-based arrays but DON’T DO IT  ReDim (Visual Basic) statement changes the size of an array while an application is running  It’s not supported in C#  We need to use Array.CopyTo Refer to frmMain.btnCopyArray_Click

Declaring Arrays (1)

 Declare a dynamic one-dimensional array with no initial elements private int[] Alist;  Declare a one-dimensional array with 10 elements (subscripts values are 0 through 9) private int[] Alist = new int[9];  Declare a one-dimensional array and initialize it private int[] Alist = new int[] {1, 2, 3, 4};

Declaring Arrays (2)

 Declare a dynamic two-dimensional array with no initial elements private int A2DList[,];  Declare a two-dimensional array with 100 elements private int[] A2DList = new int[9,9];  Declare a three-dimensional array with 8 (222) elements private int[,,] A3DList = new int[1,1,1];

Determining Array Bounds (Example)

 Get the lower and upper bound of a one- dimensional array int[] Demo = new int[] {1, 2, 3, 4, 5, 6} System.Console.WriteLine( Demo.GetLowerBound(0)) System.Console.WriteLine( Demo.GetUpperBound(0))

Referencing Array Members

 Declare a one-dimensional array and store a value in the 0th^ element int[] Alist = new int[3]; Alist[0] = 3 ;  Declare a two-dimensional array and store a value in the 0th^ element (row 0, column 0) int[,] A2DList = new int[9,9]; A2DList(0, 0) = 3;

Arrays (Characteristics 2)

 Arrays work like collections in that they can be enumerated using a foreach loop  If the array has multiple dimensions, each element will be examined in row-wise order

foreach(int Current in DemoArray1) { txtDemoArray1.AppendText( _ Current.ToString() + “\n”) } Next

Refer to frmMain.cs PrintArray

Arrays

(Informational Members)

GetLength returns the number of elements in a particular dimension  Length gets the number of elements in all dimensions  GetUpperBound gets the largest subscript in a dimension  GetLowerBound get the smallest subscript in a dimension

 Note that support has been added for arrays with Long subscripts

Copying Array Elements (2)

 Call the CopyTo method on the source array  The first argument contains the destination array  The second argument contains the starting index of the destination array where elements will be copied  The destination array must have enough room to copy store the copied elements

Array.CopyTo (Example)

 Copy DemoArray1 to DemoArray2 (We assume 1-dimensional arrays)  Note the array is initialized using dynamic value rather than a constant integer

DemoArray2 = new double[DemoArray1.GetUpperBound(0)]; DemoArray1.CopyTo(DemoArray2, 0)

Arrays (Sorting – Example)

 Sort arrays named DemoArray1 and DemoArray

System.Array.Sort(DemoArray1) System.Array.Sort(DemoArray2)

Arrays (Reinitializing)

 The Clear method reinitializes array elements  The first argument contains the array to clear  The second argument contains the starting subscript  The third argument contains the number of elements  Note the array’s size is not changed