You are on page 1of 29

OBJECT

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.

Like other programming languages, array in C# is a group of


similar types of elements that have contiguous memory location.
In C#, array is an object of base type System.Array. In C#, array
index starts from 0. We can store only fixed set of elements in C#
array.

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

The preceding figure shows the array declaration and allocation


syntax for a single-dimension array having a specific type and
length. The declaration begins with the array element type. The
element of an array can be a value type or reference type. The
element is followed by a set of empty brackets. Single-dimension
arrays use one set of brackets. The element type, plus the
brackets, yields an array type. This array type is followed by an
identifier that declares the name of the array.
MODULE 6 - ARRAYS
Example:
1) int[ ] myArrays = new int[3];

2) float[ ] myArrays;

3) myArrays=new float[6];

4) String[ ] arry = new String[8];

5) int[ ] arry = new int[5] { 15, 3, 7, 8, 9 };

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};

2) string[ ] arr = {"A", "J", "A", "Y"};

3) bool[ ] arr = {true, false};

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

You might also like