Professional Documents
Culture Documents
ORIENTED
LANGUAGE
Prof. EDGARDO T. CRUZ
MODULE 6 - ARRAYS
Intended Learning Outcomes:
At the end of this module, the students should be able to:
1. Learn array basics;
2. Declare arrays and perform compile-time initialization of array
elements;
3. Access elements of an array;
4. Become familiar with methods of the Array class;
5. Write methods that use arrays as parameters; and
6. Work through a programming example that illustrates the
lesson’s concepts
MODULE 6 - ARRAYS
ARRAYS
Arrays are powerful data structures for solving many programming
problems.
MODULE 6 - ARRAYS
Advantages
Code Optimization (less code)
Random Access
Easy to traverse data
Easy to manipulate data
Easy to sort data etc.
Disadvantages
Fixed size
MODULE 6 - ARRAYS
C# Array Types
There are 3 types of arrays in C# programming:
1. Single Dimensional Array
2. Multidimensional Array
3. Jagged Array
MODULE 6 - ARRAYS
Declaration Arrays
2) float[ ] myArrays;
3) myArrays=new float[6];
MODULE 6 - ARRAYS
Code Snippet:
MODULE 6 - ARRAYS
Code Snippet:
MODULE 6 - ARRAYS
Array using Literal Values
1) int[ ] arr = {1,2,3,4,5};
MODULE 6 - ARRAYS
Code Snippet:
MODULE 6 - ARRAYS
Code Snippet:
MODULE 6 - ARRAYS
Code Snippet:
Reference Type
A Reference Type of an array
can be implemented by
creating a custom class. A
variable of a reference type
does not contain its data
directly.
MODULE 6 - ARRAYS
Passing Array to Function
Code Snippet:
MODULE 6 - ARRAYS
Code Snippet:
MODULE 6 - ARRAYS
Code Snippet:
MODULE 6 - ARRAYS
Create an array program which prints maximum number in an array
using function.
MODULE 6 - ARRAYS
Solution:
Array program which prints maximum number in an array using
function.
MODULE 6 - ARRAYS
Array Exception Handling
MODULE 6 - ARRAYS
Multi-dimensional Arrays
The multi-dimensional array is also known as rectangular arrays in
C#. It can be two dimensional or three dimensional. The data is
stored in tabular form (row * column) which is also known as matrix.
MODULE 6 - ARRAYS
Multi-dimensional array in C# which declares, initializes
and traverse two dimensional array.
Code Snippet:
MODULE 6 - ARRAYS
Multi-dimensional array which initializes array at the time
of declaration.
Code Snippet:
MODULE 6 - ARRAYS
Jagged Arrays
In C#, jagged array is also known as "array of arrays" because its
elements are arrays. The element size of jagged array can be
different.
i.e.
arr[0] = new int[4] { 11, 21, 56, 78 };
arr[1] = new int[6] { 42, 61, 37, 41, 59, 63 };
MODULE 6 - ARRAYS
Code Snippet:
MODULE 6 - ARRAYS
Code Snippet:
MODULE 6 - ARRAYS
Params
In C#, params is a keyword which is used to specify a parameter
that takes variable number of arguments. It is useful when we don't
know the number of arguments prior.
Only one params keyword is allowed and no additional parameter
is permitted after params keyword in a function declaration.
MODULE 6 - ARRAYS
Code Snippet:
MODULE 6 - ARRAYS
Code Snippet:
MODULE 6 - ARRAYS
QUESTIONS?
MODULE 6 - ARRAYS