C# Arrays Presentation - Computer Science and Engineering Department
BhuvaneswaranB1
9 views
67 slides
Sep 03, 2024
Slide 1 of 67
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
About This Presentation
C# Arrays Presentation - CSE Department
Size: 1.15 MB
Language: en
Added: Sep 03, 2024
Slides: 67 pages
Slide Content
B.Bhuvaneswaran, AP (SG) / CSE
9791519152 [email protected]
C# and .NET Programming
Arrays
Arrays Rajalakshmi Engineering College 2
Introduction
▪Array is a group of multiple values of same type.
▪Arrays are stored in continuous-memory-locations in 'heap'.
Arrays Rajalakshmi Engineering College 3
Example
Arrays Rajalakshmi Engineering College 4
Syntax
▪type[ ] arrayReferenceVariableName = new type[ size ];
Arrays Rajalakshmi Engineering College 5
Note
▪Each value of array is called "element".
▪All the elements are stored in continuous memory locations.
▪The address of first element of the array will be stored in the
"array reference variable".
▪The "Length" property stores count of elements of array. Index
starts from 0 (zero).
▪Arrays are treated as objects of "System.Array" class; so arrays are
stored in heap; its address (first element's address) is stored in
reference variable at stack.
Arrays Rajalakshmi Engineering College 6
Array 'for' loop
▪For loop starts with an "initialization"; checks the condition;
executes the loop body and then performs incrementation or
decrementation;
▪Use "for loop" to read elements from an array, based on index.
Arrays Rajalakshmi Engineering College 7
Example
Arrays Rajalakshmi Engineering College 8
Syntax
for(inti = 0; i < arrayRefVariable.Length; i++)
{
arrayRefVariable[i]
}
Arrays Rajalakshmi Engineering College 9
Pros of Array 'for' loop
▪You can read any part of the array (all elements, start from specific
element, end with specific element).
▪You can read array elements in reverse.
Arrays Rajalakshmi Engineering College 10
Cons of Array 'for' loop
▪A bit complex syntax.
Arrays Rajalakshmi Engineering College 11
Array 'foreach' loop
▪Foreach loop starts contains a "iteration variable"; reads each
value of an array or collection and assigns to the "iteration
variable", till end of the array / collection.
▪"Foreach loop" is not based on index; it is based on sequence.
Arrays Rajalakshmi Engineering College 12
Syntax
foreach (DataType iterationVariable in arrayVariable)
{
iterationVariable
}
Arrays Rajalakshmi Engineering College 13
Pros of Array 'Foreach' loop
▪Simplified Syntax
▪Easy to use with arrays and collections.
▪It internally uses "Iterators".
Arrays Rajalakshmi Engineering College 14
Cons of Array 'Foreach' loop
▪Slower performance, due to it treats everything as a collection.
▪It can't be used to execute repeatedly, without arrays or
collections.
▪It can't read part of array / collection, or read array / collection
reverse.
Arrays Rajalakshmi Engineering College 15
'System.Array' class
▪Every array is treated as an object for System.Array class.
▪The System.Array class provides a set of properties and methods
for every array.
Arrays Rajalakshmi Engineering College 16
Properties of 'System.Array' class
▪Length
Arrays Rajalakshmi Engineering College 17
Methods of 'System.Array' class
1.IndexOf
2.BinarySearch
3.Clear
4.Resize
5.Sort
6.Reverse
7.CopyTo
8.Clone
Arrays Rajalakshmi Engineering College 18
Array - IndexOf( ) method
▪This method searches the array for the given value.
•If the value is found, it returns its index.
•If the value is not found, it returns -1.
Arrays Rajalakshmi Engineering College 19
Example
Arrays Rajalakshmi Engineering College 20
Signature
▪static int Array.IndexOf( System.Array array, object value)
Arrays Rajalakshmi Engineering College 21
Example
▪Array.IndexOf(array, value3) = 3
Arrays Rajalakshmi Engineering College 22
Explanation
▪The “IndexOf” method performs linear search. That means it
searches all the elements of an array, until the search value is
found. When the search value is found in the array, it stops
searching and returns its index.
▪The linear search has good performance, if the array is small. But if
the array is larger, Binary search is recommended to improve the
performance.
Arrays Rajalakshmi Engineering College 23
Parameters
▪array
•This parameter represents the array, in which you want to search.
▪value
•This parameter represents the actual value that is to be searched.
▪startIndex
•This parameter represents the start index, from where the search should be
started.
Arrays Rajalakshmi Engineering College 24
Array - BinarySearch( ) method
▪This method searches the array for the given value.
•If the value is found, it returns its index.
•If the value is not found, it returns -1.
Arrays Rajalakshmi Engineering College 25
Example
Arrays Rajalakshmi Engineering College 26
Signature
▪static int Array.BinarySearch( System.Array array, object value)
Arrays Rajalakshmi Engineering College 27
Example
▪Array.BinarySearch(array, value3) = 3
Arrays Rajalakshmi Engineering College 28
Explanation
▪The “Binary Search” requires an array, which is already sorted. On
unsorted arrays, binary search is not possible.
▪It directly goes to the middle of the array (array size / 2), and
checks that item is less than / greater than the search value.
▪If that item is greater than the search value, it searches only in the
first half of the array.
▪If that item is less than the search value, it searches only in the
second half of the array.
▪Thus it searches only half of the array. So in this way, it saves
performance.
Arrays Rajalakshmi Engineering College 29
Parameters
▪array
•This parameter represents the array, in which you want to search.
▪value
•This parameter represents the actual value that is to be searched
Arrays Rajalakshmi Engineering College 30
Array - Clear( ) method
▪This method starts with the given index and sets all the “length”
no. of elements to zero (0).
Arrays Rajalakshmi Engineering College 31
Example
Arrays Rajalakshmi Engineering College 32
Signature
▪static void Array.Clear( System.Array array, int index, int length)
Arrays Rajalakshmi Engineering College 33
Example
▪Array.Clear(a, 2, 3)
Arrays Rajalakshmi Engineering College 34
Parameters
▪array
•This parameter represents the array, in which you want to clear the
elements.
▪index
•This parameter represents the index, from which clearing process is to be
started.
▪length
•This parameter represents the no. of elements that are to be cleared.
Arrays Rajalakshmi Engineering College 35
Array - Resize( ) method
▪This method increases / decreases size of the array.
Arrays Rajalakshmi Engineering College 36
Signature
▪static void Array.Resize(ref System.Array array, int newSize)
Arrays Rajalakshmi Engineering College 37
Example
▪Array.Resize(a, 6)
Arrays Rajalakshmi Engineering College 38
Parameters
▪array
•This parameter represents the array, which you want to resize.
▪newSize
•This parameter represents the new size of the array, how many elements
you want to store in the array. It can be less than or greater than the
current size.
Arrays Rajalakshmi Engineering College 39
Array - Sort( ) method
▪This method sorts the array in ascending order.
Arrays Rajalakshmi Engineering College 45
Example
▪Array.Reverse(a)
Arrays Rajalakshmi Engineering College 46
Types of Arrays
▪Single-Dim Arrays
▪Multi-Dim Arrays
Arrays Rajalakshmi Engineering College 47
Single-Dim Arrays
▪Group of multiple rows; each row contains a single value.
Arrays Rajalakshmi Engineering College 48
Example
Arrays Rajalakshmi Engineering College 49
Multi-Dim Arrays
▪Group of multiple rows; each row contains a group of values.
Arrays Rajalakshmi Engineering College 50
Example
Arrays Rajalakshmi Engineering College 51
Multi-Dim Arrays
▪Stores elements in rows & columns format.
▪Every row contains a series of elements.
▪You can create arrays with two or dimensions, by increasing the
no. of commas (,).
Arrays Rajalakshmi Engineering College 54
Jagged Arrays
▪Jagged Array is an “array of arrays”.
▪The member arrays can be of any size.
Arrays Rajalakshmi Engineering College 55
Example
Arrays Rajalakshmi Engineering College 56
Syntax
▪type[ ] [ ] arrayReferenceVariable = new type[ rowSize ] [ ];
▪arrayReferenceVariable[index] = new type[ size ];
Arrays Rajalakshmi Engineering College 57
Array - CopyTo( ) method
▪This method copies (shallow copy) all the elements from source
array to destination array, starting from the specified 'startIndex'.
Arrays Rajalakshmi Engineering College 58
Example
Arrays Rajalakshmi Engineering College 59
Signature
▪void Array.CopyTo(System.Array destinationArray, int startIndex)
Arrays Rajalakshmi Engineering College 60
Parameters
▪sourceArray
•This parameter represents the array, which array you want to copy.
▪destinationArray
•This parameter represents the array, into which you want to copy the
elements of source array. The destination array must exist already and
should be large enough to hold new values.
▪startIndex
•This parameter represents the index of the element, from which you want
to start copying.
Arrays Rajalakshmi Engineering College 61
Array - Clone( ) method
Arrays Rajalakshmi Engineering College 62
Signature
▪object System.Array.Clone()
Arrays Rajalakshmi Engineering College 63
Array.CopyTo()
▪CopyTo() requires to have an existing destination array; and the
destination array should be large enough to hold all elements
from the source array, starting from the specified startIndex.
▪CopyTo() allows you to specify the startIndex at destination array.
▪The result array need not be type-casted explicitly.
Arrays Rajalakshmi Engineering College 64
Array.Clone()
▪Clone() creates a new destination array; you need not have an
existing array.
▪Clone() doesn't allow you to specify the startIndex at destination
array.
▪The result array will be returned as 'object' type; so need to be
type-casted to array type.
Arrays Rajalakshmi Engineering College 65
Assignment
1.What is a deep copy of an array and how is it different from a shallow copy?
2.Explain the usage of Array.Copy() method in C# and how it differs from Clone()
method.
3.Explain the usage and benefits of the foreach loop in C#.
4.What are some common use cases for using arrays in real-world projects?
5.What are the advantages and disadvantages of using arrays in C#?
6.How can you efficiently iterate over the elements of an array in C# using a
foreach loop?
7.What is a jagged array in C#? How is it different from a multi-dimensional
array?
8.What are some commonly used built-in methods of the System.Array class in
C#?
9.What is the difference between IndexOf() and BinarySearch() methods in
System.Array class?