Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

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

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

2012/2013

Uploaded on 09/27/2013

vikrant
vikrant 🇮🇳

4.4

(9)

130 documents

1 / 64

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Working With C# Arrays
And .NET Lists
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40

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