Itec103-Module2 2022
Itec103-Module2 2022
Learning Outcomes
Intended Students should be able to meet the following intended learning outcomes:
Learning Understand the use of Array in C# Programming.
Outcomes Understand how to declare Array in C#.
Identify the difference of Program using one dimensional and two dimensional
Array.
Understand the definition of stacks and queues.
Understand the declaration of stacks and queues.
Understand and learn to Add and remove of stack and queues in C#.
Understand the programs using Stacks and Queues.
Discuss and Learn Different Object-Oriented Programming
Discuss the OOP important features
Discuss and learn the differences between Abstraction and Encapsulation
(For further instructions, refer to your Google Classroom and see the
schedule of activities for this module)
Note: The insight that you will post on online discussion forum using Learning Management
System (LMS) will receive additional scores in class participation.
Lecture Guide:
ARRAY
What is an Array?
An array stores a fixed-size sequential collection of elements of the same type. An array is
used to store a collection of data, but it is often more useful to think of an array as a
collection of variables of the same type stored at contiguous memory locations.
• All arrays consist of contiguous memory locations. The lowest address corresponds
to the first element and the highest address to the last element.
• Declaring Arrays
Offline Activities • To declare an array in C#, you can use the following syntax −
(e-Learning/Self-Paced)
datatype[] arrayName;
where,
• [ ] specifies the rank of the array. The rank specifies the size of the array.
For example,
double[] balance;
• Initializing an Array
• Declaring an array does not initialize the array in the memory. When the array
variable is initialized, you can assign values to the array.
balance[0] = 4500.0;
You can assign values to the array at the time of declaration, as shown −
You can copy an array variable into another target array variable. In such case, both the
target and source point to the same memory location −
An element is accessed by indexing the array name. This is done by placing the index of the
element within square brackets after the name of the array. For example,
using System;
namespace ArrayApplication {
class MyArray {
int i,j;
n[ i ] = i + 100;
Console.ReadKey();
• When the above code is compiled and executed, it produces the following result −
Element[0] = 100
Element[1] = 101
Element[2] = 102
Element[3] = 103
Element[4] = 104
Element[5] = 105
Element[6] = 106
Element[7] = 107
Element[8] = 108
Element[9] = 109
• Using the foreach Loop
In the previous example, we used a for loop for accessing each array element. You can also
use a foreach statement to iterate through an array.
using System;
namespace ArrayApplication {
class MyArray {
n[i] = i + 100;
foreach (int j in n ) {
int i = j-100;
Console.ReadKey();
When the above code is compiled and executed, it produces the following result −
Element[0] = 100
Element[1] = 101
Element[2] = 102
Element[3] = 103
Element[4] = 104
Element[5] = 105
Element[6] = 106
Element[7] = 107
Element[8] = 108
Element[9] = 109
• C# Arrays
• There are following few important concepts related to array which should be clear
Multi-dimensional arrays
int [ , , ] m;
• Two-Dimensional Arrays
A 2-dimensional array can be thought of as a table, which has x number of rows and y
number of columns. Following is a 2-dimensional array, which contains 3 rows and 4
columns −
• Thus, every element in the array a is identified by an element name of the form a[ i
, j ], where a is the name of the array, and i and j are the subscripts that uniquely
identify each element in array a.
Multidimensional arrays may be initialized by specifying bracketed values for each row.
The Following array is with 3 rows and each row has 4 columns.
};
An element in 2-dimensional array is accessed by using the subscripts. That is, row index
and column index of the array.
For example,
using System;
namespace ArrayApplication {
class MyArray {
int i, j;
Console.ReadKey();
a[0,0]: 0
a[0,1]: 0
a[1,0]: 1
a[1,1]: 2
a[2,0]: 2
a[2,1]: 4
a[3,0]: 3
a[4,0]: 4
a[4,1]: 8
Jagged arrays
A Jagged array is an array of arrays. You can declare a jagged array named scores of
type int as −
Declaring an array, does not create the array in memory. To create the above array −
Where, scores is an array of two arrays of integers - scores[0] is an array of 3 integers and
scores[1] is an array of 4 integers.
using System;
namespace ArrayApplication {
class MyArray {
int i, j;
Console.ReadKey();
You can pass to the function a pointer to an array by specifying the array's name without
an index.
You can pass an array as a function argument in C#. The following example demonstrates
this −
using System;
namespace ArrayApplication {
class MyArray {
int i;
double avg;
int sum = 0;
sum += arr[i];
return avg;
double avg;
avg = app.getAverage(balance, 5 ) ;
Console.ReadKey();
Param arrays
At times, while declaring a method, you are not sure of the number of arguments passed
as a parameter. C# param arrays (or parameter arrays) come into help at such times.
using System;
namespace ArrayApplication {
class ParamArray {
int sum = 0;
sum += i;
return sum;
class TestClass {
Console.ReadKey();
Defined in System namespace, it is the base class to all arrays, and provides various
properties and methods for working with arrays.
The Array class is the base class for all the arrays in C#. It is defined in the System
namespace. The Array class provides various properties and methods to work with arrays.
The following table describes some of the most commonly used properties of the Array
class −
using System;
namespace ArrayApplication {
class MyArray {
Console.WriteLine();
Array.Reverse(temp);
Console.WriteLine();
Array.Sort(list);
Console.WriteLine();
Console.ReadKey();
What is a STACK?
The stack is a special case collection which represents a last in first out (LIFO) concept. To
first understand LIFO, let's take an example. Imagine a stack of books with each book kept
on top of each other.
The concept of last in first out in the case of books means that only the top most book can
be removed from the stack of books. It is not possible to remove a book from between,
because then that would disturb the setting of the stack.
• Hence in C#, the stack also works in the same way. Elements are added to the
stack, one on the top of each other. The process of adding an element to the stack
is called a push operation. To remove an element from a stack, you can also
remove the top most element of the stack. This operation is known as pop.
• Let's look at the operations available for the Stack collection in more detail.
A stack is created with the help of the Stack Data type. The keyword "new" is used to
create an object of a Stack. The object is then assigned to the variable st.
The push method is used to add an element onto the stack. The general syntax of the
statement is given below.
Stack.push(element)
The pop method is used to remove an element from the stack. The pop operation will
return the topmost element of the stack. The general syntax of the statement is given
below
• Count
This property is used to get the number of items in the Stack. Below is the general syntax
of this statement.
Stack.count
• Contains
This method is used to see if an element is present in the Stack. Below is the general
syntax of this statement. The statement will return true if the element exists, else it will
return the value false.
Stack.Contains(element)
• Now let's see this working at a code level. All of the below-mentioned code will be
written to our Console application. The code will be written to our Program.cs file.
• In the below program, we will write the code to see how we can use the above-
mentioned methods.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DemoApplication
class Program
st.Push(1);
st.Push(2);
st.Push(3);
Console.WriteLine(obj);
Console.WriteLine(); Console.WriteLine();
Console.ReadKey();
1.The first step is used to declare the Stack. Here we are declaring "st" as a variable to hold
the elements of our stack.
2.Next, we add 3 elements to our stack. Each element is added via the Push method.
3.Now since the stack elements cannot be accessed via the index position like the array
list, we need to use a different approach to display the elements of the stack. The Object
(obj) is a temporary variable, which is declared for holding each element of the stack. We
then use the foreach statement to go through each element of the stack. For each stack
element, the value is assigned to the obj variable. We then use the Console.Writeline
command to display the value to the console.
4.We are using the Count property (st.count) to get the number of items in the stack. This
property will return a number. We then display this value to the console.
5.We then use the Contains method to see if the value of 3 is present in our stack. This will
return either a true or false value. We then display this return value to the console.
If the above code is entered properly and the program is run the following output will be
displayed.
• Example 2
• Now let's look at the "remove" functionality. We will see the code required to
remove the topmost element from the stack.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
namespace DemoApplication
class Program
st.Push(1);
st.Push(2);
st.Push(3);
st.Pop();
Console.WriteLine(obj);
Console.ReadKey();
• QUEUES
The Queue is a special case collection which represents a first in first out concept. Imagine
a queue of people waiting for the bus. Normally, the first person who enters the queue will
be the first person to enter the bus. Similarly, the last person to enter the queue will be
the last person to enter into the bus. Elements are added to the queue, one on the top of
• Let's look at the operations available for the Queue collection in more detail.
The declaration of a Queue is provided below. A Queue is created with the help of the
Queue Data type. The "new" keyword is used to create an object of a Queue. The object is
then assigned to the variable qt.
The enqueue method is used to add an element onto the queue. The general syntax of the
statement is given below.
Queue.enqueue(element)
The dequeue method is used to remove an element from the queue. The dequeue
operation will return the first element of the queue. The general syntax of the statement is
given below
Queue.dequeue()
• Count
This property is used to get the number of items in the queue. Below is the general syntax
of this statement.
Queue.Count
• Contains
This method is used to see if an element is present in the Queue. Below is the general
syntax of this statement. The statement will return true if the element exists, else it will
return the value false.
Queue.Contains(element)
• Now, let's see this working at a code level. All of the below-mentioned code will be
written to our Console application.
• The code will be written to our Program.cs file. In the below program, we will write
the code to see how we can use the above-mentioned methods.
• In this example, we will see how a queue gets created. Next, we will see how to
display the elements of the queue, and use the Count and Contain methods.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DemoApplication
class Program
qt.Enqueue(1);
qt.Enqueue(2);
qt.Enqueue(3);
Console.WriteLine(obj);
Console.WriteLine(); Console.WriteLine();
Console.ReadKey();
• Code Explanation
1.The first step is used to declare the Queue. Here we are declaring qt as a variable to hold
the elements of our Queue.
2.Next, we add 3 elements to our Queue. Each element is added via the "enqueue"
method.
3. Now one thing that needs to be noted about Queues is that the elements cannot be
accessed via the index position like the array list. We need to use a different approach to
display the elements of the Queue. So here's how we go about displaying the elements of
a queue.
• We first declare a temporary variable called obj. This will be used to hold each
element of the Queue.
• We then use the foreach statement to go through each element of the Queue.
• For each Queue element, the value is assigned to the obj variable.
• We then use the Console.Writeline command to display the value to the console.
4. We are using the "Count" property to get the number of items in the Queue. This
property will return a number. We then display this value to the console.
5. We then use the "Contains" method to see if the value of 3 is present in our Queue. This
will return either a true or false value. We then display this return value to the console.
If the above code is entered properly and the program is run the following output will be
displayed.
• C# Queue Dequeue
• Now let's look at the remove functionality. We will see the code required to
remove the last element from the queue.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
class Program
qt.Enqueue(1);
qt.Enqueue(2);
qt.Enqueue(3);
qt.Dequeue();
Console.WriteLine(obj);
Console.ReadKey();
• Code Explanation
1. Here we just issue the "dequeue" method, which is used to remove an element from the
queue. This method will remove the first element of the queue.
• If the above code is entered properly and the program is run the following output
will be displayed.
• SUMMARY
• A Stack is based on the last in first out concept. The operation of adding an
element to the stack is called the push operation. The operation of removing an
element to the stack is called the pop operation.
• A Queue is based on the first in first out concept. The operation of adding an
element to the queue is called the enqueue operation. The operation of removing
WHAT IS OOP?
Object Oriented Programming (OOP) is one of the most popular programming languages.
This article is an introduction to Object Oriented Programming (OOP) and how to
implement OOP in C# including abstraction, encapsulation, inheritance and
polymorphism.
FEATURES
OOP allows decomposition of a problem into a number of entities called objects and then
builds data and functions around these objects.
1. The software is divided into a number of small units called objects. The data and
functions are built around these objects.
2. The data of the objects can be accessed only by the functions associated with that
object.
3. The functions of one object can access the functions of another object.
CLASSES
A class is the core of any modern Object Oriented Programming language such as C#.
In OOP languages it is mandatory to create a class for representing data.
A class is a blueprint of an object that contains variables for storing data and functions to
perform operations on the data.
A class will not occupy any memory space and hence it is only a logical representation of
data.
To create a class, you simply use the keyword "class" followed by the class name:
class Employee
{
}
Object
Objects are the basic run-time entities of an object oriented system. They may represent a
person, a place or any item that the program must handle.
A class will not occupy any memory space. Hence to work with the data
represented by the class you must create a variable for the class, that is called an
object.
When an object is created using the new operator, memory is allocated for the
class in the heap, the object is called an instance and its starting address will be
When an object is created without the new operator, memory will not be
allocated in the heap, in other words an instance will not be created and the
object in the stack contains the value null.
When an object contains null, then it is not possible to access the members of the
class using that object.
class Employee
{
}
Abstraction
Abstraction is "To represent the essential feature without representing the background
details."
Abstraction lets you focus on what the object does instead of how it does it.
Abstraction is the process of hiding the working style of an object, and showing the
information of an object in an understandable manner.
Abstract information (necessary and common information) for the object "Mobile
Phone" is that it makes a call to any number and can send SMS.
So that, for a mobile phone object you will have the abstract class as in the
following,
abstract class MobilePhone {
public void Calling();
public void SendSMS();
}
public class Nokia1400: MobilePhone {}
public class Nokia2700: MobilePhone {
public void FMRadio();
public void MP3();
public void Camera();
}
public class BlackBerry: MobilePhone {
public void FMRadio();
public void MP3();
public void Camera();
public void Recording();
public void ReadAndSendEmails();
}
Abstraction means putting all the variables and methods in a class that are
necessary.
For example: Abstract class and abstract method.
Abstraction is a common thing.
Example
If somebody in your college tells you to fill in an application form, you will provide
your details, like name, address, date of birth, which semester, percentage you
have etcetera.
If some doctor gives you an application to fill in the details, you will provide the
details, like name, address, date of birth, blood group, height and weight.
See in the preceding example what is in common?
Age, name and address, so you can create a class that consists of the common
data. That is called an abstract class.
That class is not complete and it can be inherited by other classes.
Encapsulation
Wrapping up a data member and a method together into a single unit (in other
words class) is called Encapsulation.
Encapsulation is like enclosing in a capsule. That is enclosing the related
operations and data related to an object into that object.
Encapsulation is like your bag in which you can keep your pen, book etcetera. It
means this is the property of encapsulating members and functions.
class Bag {
book;
pen;
ReadBook();
}
Encapsulation means hiding the internal details of an object, in other words how an object
does something.
Encapsulation prevents clients from seeing it’s inside view, where the behavior of the
abstraction is implemented.
Hide the data for security such as making the variables private, and expose the property to
access the private data that will be public. So, when you access the property you can
validate the data and set it.
Example
class Demo {
private int _mark;
public int Mark {
get {
return _mark;
}
set {
if (_mark > 0) _mark = value;
else _mark = 0;
}
}
}
This means that you are creating the class with functions and with objects
(capsules) of which you are making available the functionality of your class by that
object and without the interference in the original class.
Example 2
TV operation
It is encapsulated with a cover and we can operate it with a remote and there is no
need to open the TV to change the channel.
Here everything is private except the remote, so that anyone can access the
remote to operate and change the things in the TV.
Inheritance
Output
Parent Constructor.
Child Constructor.
I'm a Parent Class.
Polymorphism
Example 1
Example 2
A person behaves the son in a house at the same time that the person behaves as
an employee in an office.
Example 3
Your mobile phone, one name but many forms:
As phone
As camera
As mp3 player
As radio
Real-world Example
But how does the Mobile Phone work internally? How are the keypad buttons
connected to the internal circuit? That is called Encapsulation.
Summary
"Encapsulation is accomplished using classes. Keeping data and methods that
access that data into a single unit."
"Abstraction is accomplished using an Interface. Just giving the abstract
information about what it can do without specifying the details."
"Information/Data hiding is accomplished using modifiers by keeping the instance
variables private or protected."
Performance Tasks
1. What OOP?
2. Write the basic concept of OOP?
3. What is class?
4. What is an object?
5. What are manipulators?
6. What is the main difference between a class and an object?
7. What is the default access modifier in a class?
8. What is an accessibility modifier and how many are there in C#?
9. Can “this “keyword is used within a static method?
10. How can you implement multiple inheritance in C#?
11. Are private class members inherited to the derived class?
12. Can you create object of class with private constructor n C#?
13. When do you use abstract class?
14. What are the fundamental principles of OO programming?
15. How does OO simplify development?
Laboratory Activity
1. Write a program of sorting an array. Declare single dimensional array and accept 5 integer values from the user.
Then sort the input in ascending order and display output.
2. Write a program to create two multidimensional arrays of same size. Accept value from user and store them in
first array. Now copy all the elements of first array are second array and print output.
Accuracy of research Contents gathered are Most of the contents Contents gathered 0. 2
contents limited, or are are correct. are all accurate.
imprecise
TOTAL /50
Learning Resources
https://www.completecsharptutorial.com/basic/array-examples.php
https://www.c-sharpcorner.com/blogs/how-to-use-queue-and-stacks-in-c-sharp
https://www.w3schools.com/cs/default.asp