The document provides an overview of arrays in C# and .NET, detailing their structure, syntax, and various methods associated with the System.Array class. It covers different types of arrays, including single-dimensional, multi-dimensional, and jagged arrays, as well as the pros and cons of using 'for' and 'foreach' loops for iteration. Additionally, it explains key methods like IndexOf, BinarySearch, Clear, Resize, Sort, Reverse, CopyTo, and Clone, along with practical examples and usage scenarios.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
3 views67 pages
Arrays
The document provides an overview of arrays in C# and .NET, detailing their structure, syntax, and various methods associated with the System.Array class. It covers different types of arrays, including single-dimensional, multi-dimensional, and jagged arrays, as well as the pros and cons of using 'for' and 'foreach' loops for iteration. Additionally, it explains key methods like IndexOf, BinarySearch, Clear, Resize, Sort, Reverse, CopyTo, and Clone, along with practical examples and usage scenarios.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 67
C# and .
NET Programming
Arrays
B.Bhuvaneswaran, AP (SG) / CSE
9791519152 [email protected] Introduction Array is a group of multiple values of same type. Arrays are stored in continuous-memory-locations in 'heap'.
Arrays Rajalakshmi Engineering College 2
Example
Arrays Rajalakshmi Engineering College 3
Syntax type[ ] arrayReferenceVariableName = new type[ size ];
Arrays Rajalakshmi Engineering College 4
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 5
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 6
Example
Arrays Rajalakshmi Engineering College 7
Syntax for (int i = 0; i < arrayRefVariable.Length; i++) { arrayRefVariable[i] }
Arrays Rajalakshmi Engineering College 8
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 9
Cons of Array 'for' loop A bit complex syntax.
Arrays Rajalakshmi Engineering College 10
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 11
Syntax foreach (DataType iterationVariable in arrayVariable) { iterationVariable }
Arrays Rajalakshmi Engineering College 12
Pros of Array 'Foreach' loop Simplified Syntax Easy to use with arrays and collections. It internally uses "Iterators".
Arrays Rajalakshmi Engineering College 13
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 14
'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 15
Properties of 'System.Array' class Length
Arrays Rajalakshmi Engineering College 16
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 17
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 18
Example
Arrays Rajalakshmi Engineering College 19
Signature static int Array.IndexOf( System.Array array, object value)
Arrays Rajalakshmi Engineering College 20
Example Array.IndexOf(array, value3) = 3
Arrays Rajalakshmi Engineering College 21
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 22
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 23
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 24
Example
Arrays Rajalakshmi Engineering College 25
Signature static int Array.BinarySearch( System.Array array, object value)
Arrays Rajalakshmi Engineering College 26
Example Array.BinarySearch(array, value3) = 3
Arrays Rajalakshmi Engineering College 27
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 28
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 29
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 30
Example
Arrays Rajalakshmi Engineering College 31
Signature static void Array.Clear( System.Array array, int index, int length)
Arrays Rajalakshmi Engineering College 32
Example Array.Clear(a, 2, 3)
Arrays Rajalakshmi Engineering College 33
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 34
Array - Resize( ) method This method increases / decreases size of the array.
Arrays Rajalakshmi Engineering College 35
Signature static void Array.Resize(ref System.Array array, int newSize)
Arrays Rajalakshmi Engineering College 36
Example Array.Resize(a, 6)
Arrays Rajalakshmi Engineering College 37
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 38
Array - Sort( ) method This method sorts the array in ascending order.
Types of Arrays Single-Dim Arrays Multi-Dim Arrays
Arrays Rajalakshmi Engineering College 46
Single-Dim Arrays Group of multiple rows; each row contains a single value.
Arrays Rajalakshmi Engineering College 47
Example
Arrays Rajalakshmi Engineering College 48
Multi-Dim Arrays Group of multiple rows; each row contains a group of values.
Arrays Rajalakshmi Engineering College 49
Example
Arrays Rajalakshmi Engineering College 50
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 (,).
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 57
Example
Arrays Rajalakshmi Engineering College 58
Signature void Array.CopyTo(System.Array destinationArray, int startIndex)
Arrays Rajalakshmi Engineering College 59
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 60
Array - Clone( ) method
Arrays Rajalakshmi Engineering College 61
Signature object System.Array.Clone()
Arrays Rajalakshmi Engineering College 62
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 63
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 64
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?