Professional Documents
Culture Documents
It is intimated that following Lectures will not be repeated and would be part
of mid-term & final exam as well.
(Week 3) Lecture # 5:
Objective:
• Inheritance
• Foreach loop
Inheritance:
One of the most important concepts in object-oriented programming is that of inheritance.
Inheritance allows us to define a class in terms of another class, which makes it easier to create
and maintain an application. This also provides an opportunity to reuse the code functionality and
fast implementation time. When there is “is a” relation between two classes that is inheritance.
When creating a class, instead of writing completely new data members and member functions,
the programmer can designate that the new class should inherit the members of an existing class.
This existing class is called the base class, and the new class is referred to as the derived class.
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
student s1 = new student();
s1.setData();
s1.showData();
}
}
}
}
}
}
}
FOREACH LOOP:
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication2
{
class Program
{
Visual programing (CS-692/CS-783)
Mr. Ikram Afzal email: ikram@biit.edu.pk Whatsapp#03345373093
Mr. Shahid Jamil email: shahid@biit.edu.pk Whatsapp#03155425199
Mr. Umar Farooq email: umar.farooq@biit.edu.pk Whatsapp#03327661819
static void Main(string[] args)
{
int[] myArray = new int[10];
}
}
}
GENERICS:
It helps us to maximize code reuse, performance and type safety.
Generics are strongly type collections.
Generics helps to decouple the data-type from the logical code snippet which makes the code to
reuse again and again
When to Use
Generics helps to separate the data-type from the logical code i.e. from methods and classes. So
if you want to maximize your code reusability and maintain its type safety, and performance then
use generics.
Generics acts as a bridge between dot.net data-types and your custom logical code and provides
flexibility to maximize the code reusability and reduces code.
EXAMPLE:
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
compare<string>("21", "21");
}
Visual programing (CS-692/CS-783)
Mr. Ikram Afzal email: ikram@biit.edu.pk Whatsapp#03345373093
Mr. Shahid Jamil email: shahid@biit.edu.pk Whatsapp#03155425199
Mr. Umar Farooq email: umar.farooq@biit.edu.pk Whatsapp#03327661819
Example 2:
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)//static function call static function only
{
int a = 10, b = 20;
swap<int>(ref a, ref b);
Console.WriteLine("A is: " + a);
Console.WriteLine("B is: " + b);
Stack
Queue
List
Array
Arrays:
An array is a collective name given to a group of similar quantities. These similar quantities
could be percentage marks of 100 students, number of chairs in home, or salaries of 300
employees or ages of 25 students.
Syntax:
Example:
int noOfElement=arrayName.Length;
int maxValue=arrayName.Max<int>();
int minValue = arrayName.Min<int>();
bool flag=arrayName.Contains(10);
STACK:
Working principle is FIRST IN LAST OUT.
Required namespace is : System.Collections.Generic;
To add value in stack push() method is used. To remove from stack pop() method is used. To
show top element peek() method is used.
C# Code:
QUEUE:
C# Code:
Queue<string> myQueue = new Queue<string>();
myQueue.Enqueue("E1");
myQueue.Peek();
myQueue.Dequeue();
myQueue.Count();
myQueue.Contains("e1");
LIST:
Required namespace is: System.Collections.Generic;
Visual programing (CS-692/CS-783)
Mr. Ikram Afzal email: ikram@biit.edu.pk Whatsapp#03345373093
Mr. Shahid Jamil email: shahid@biit.edu.pk Whatsapp#03155425199
Mr. Umar Farooq email: umar.farooq@biit.edu.pk Whatsapp#03327661819
//newList.Clear();
bool find=newList.Contains(" is ");
Console.WriteLine(find);
int noOfItem=newList.Count();
Console.WriteLine(noOfItem);
string element=newList.ElementAt(4);
Console.WriteLine(element);
newList.Remove("This");
newList.RemoveAt(0);
newList.Reverse();