C# Arrays Presentation - Computer Science and Engineering Department

BhuvaneswaranB1 9 views 67 slides Sep 03, 2024
Slide 1
Slide 1 of 67
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48
Slide 49
49
Slide 50
50
Slide 51
51
Slide 52
52
Slide 53
53
Slide 54
54
Slide 55
55
Slide 56
56
Slide 57
57
Slide 58
58
Slide 59
59
Slide 60
60
Slide 61
61
Slide 62
62
Slide 63
63
Slide 64
64
Slide 65
65
Slide 66
66
Slide 67
67

About This Presentation

C# Arrays Presentation - CSE Department


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 40
Example

Arrays Rajalakshmi Engineering College 41
Signature
▪static void System.Array.Sort( System.Array array )

Arrays Rajalakshmi Engineering College 42
Example
▪Array.Sort(a)

Arrays Rajalakshmi Engineering College 43
Array - Reverse( ) method
▪This method sorts the array in ascending order.

Arrays Rajalakshmi Engineering College 44
Signature
▪static void System.Array.Reverse( System.Array array )

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 52
Example

Arrays Rajalakshmi Engineering College 53
Syntax
▪type[ , ] arrayReferenceVariable=new type[ rowSize, columnSize ];

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?

Queries?

Thank You…!
Tags