
























































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Various types of lists available in c#, including arrays, arraylists, sortedlists, and dictionaries. It covers the declaration, initialization, bounds, examination, and copying of arrays, as well as the adding, updating, removing, finding, and enumerating of arraylists, sortedlists, and dictionaries.
Typology: Slides
1 / 64
This page cannot be seen from the preview
Don't miss anything!
Discuss arrays Discuss the various types of lists available from C# and the .NET Framework
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 (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
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};
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];
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))
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 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
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
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
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)
Sort arrays named DemoArray1 and DemoArray
System.Array.Sort(DemoArray1) System.Array.Sort(DemoArray2)
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