Unit 3 New
Unit 3 New
Contents
Classes and Structure,
Construction and Disposal of object,
Inheritance,
Method Overloading,
Operator Overloading,
Interfaces,
Collections,
Indexers,
Delegates and Events
1
21-03-2024
What is OOP?
OOP stands for Object-Oriented Programming.
In C#, Object is a real world entity, for example, chair, car, pen, mobile,
laptop etc.
In other words, object is an entity that has state and behavior. Here, state
means data and behavior means functionality.
2
21-03-2024
Object is an instance of a class. All the members of the class can be accessed
through object.
C# Class
In C#, class is a group of similar objects. It is a template from which objects
are created. It can have fields, methods, constructors etc.
3
21-03-2024
4
21-03-2024
C# Constructor
In C#, constructor is a special method which is invoked automatically at the
time of object creation. It is used to initialize the data members of new
object generally. The constructor in C# has the same name as class or struct.
Default constructor
Parameterized constructor
10
5
21-03-2024
C# Default Constructor
A constructor which has no argument is known as default constructor. It is invoked at the
time of creating object.
C# Default Constructor Example: Having Main() within class
using System;
public class Employee
{
public Employee()
{
Console.WriteLine("Default Constructor Invoked");
}
public static void Main(string[] args)
{
Employee e1 = new Employee();
Employee e2 = new Employee();
}
}
Output:
11
12
6
21-03-2024
C# Parameterized Constructor
A constructor which has parameters is called
parameterized constructor. It is used to provide
different values to distinct objects. class TestEmployee
using System;
{
public class Employee
public static void Main(string[] args)
{
public int id; {
public String name; Employee e1 = new Employee(101, "Sonoo", 890000f);
public float salary; Employee e2 = new Employee(102, "Mahesh", 490000f);
public Employee(int i, String n,float s)
e1.display();
{
e2.display();
id = i;
name = n; }
salary = s; }
} Output:
public void display()
{
101 Sonoo 890000
Console.WriteLine(id + " " + name+" "+salary);
102 Mahesh 490000
}
}
13
C# Destructor
A destructor works opposite to constructor, It destructs the objects of classes.
It can be defined only once in a class. Like constructors, it is invoked
automatically.
Note: C# destructor cannot have parameters.
14
7
21-03-2024
}
}
15
C# Inheritance
In C#, inheritance is a process in which one object acquires all the properties
and behaviors of its parent object automatically. In such way, you can reuse,
extend or modify the attributes and behaviors which is defined in other class.
In C#, the class which inherits the members of another class is called derived
class and the class whose members are inherited is called base class. The
derived class is the specialized class for the base class.
Advantage of C# Inheritance
Code reusability: Now you can reuse the members of your parent class. So,
there is no need to define the member again. So less code is required in the
class.
16
8
21-03-2024
using System;
public class Employee
{
public float salary = 40000;
}
public class Programmer: Employee
{
public float bonus = 10000;
}
class TestInheritance{
public static void Main(string[] args)
{
Programmer p1 = new Programmer();
Console.WriteLine("Salary: " + p1.salary);
Console.WriteLine("Bonus: " + p1.bonus);
}
}
Output:
Salary: 40000
Bonus: 10000
17
using System;
public class Animal
{
public void eat() { Console.WriteLine("Eating..."); }
}
public class Dog: Animal
{
public void bark() { Console.WriteLine("Barking..."); }
}
class TestInheritance2{
public static void Main(string[] args)
{
Dog d1 = new Dog();
d1.eat();
d1.bark();
}
}
Output:
Eating...
Barking...
18
9
21-03-2024
19
C# Method Overloading
Having two or more methods with same name but different in parameters, is
known as method overloading in C#.
20
10
21-03-2024
return a + b;
}
{
return a + b + c;
}
}
{
{
Console.WriteLine(Cal.add(12, 23));
}
}
Output:
35
60
21
22
11
21-03-2024
Operator Overloading In C#
In C#, a special function called operator function is used for overloading
purpose. These special function or method must be public and static. They
can take only value arguments. The ref and out parameters are not allowed
as arguments to operator functions. The general form of an operator function
is as follows.
23
24
12
21-03-2024
class Complex
{
private int x;
private int y;
class MyClient
public Complex() {
{
} public static void Main()
public Complex(int i, int j) {
{ Complex c1 = new Complex(10, 20);
x = i; c1.ShowXY(); // displays 10 & 20
y = j; Complex c2 = new Complex(20, 30);
} c2.ShowXY(); // displays 20 & 30
public void ShowXY() Complex c3 = new Complex();
{ c3 = c1 + c2;
Console.WriteLine("{0} {1}", x, y); c3.ShowXY(); // dislplays 30 & 50
} }
public static Complex operator +(Complex c1, Complex c2) }
{
Complex temp = new Complex();
temp.x = c1.x + c2.x;
temp.y = c1.y + c2.y;
return temp;
}
}
25
Example 2
class Rectangle
{
public static Rectangle operator + (Rectangle b, Rectangle c)
private int length;
{
private int breadth;
Rectangle r = new Rectangle();
private int height; r.length = b.length + c.length;
r.breadth = b.breadth + c.breadth;
public Rectangle() { } //default r.height = b.height + c.height;
constructor
return r;
}
public Rectangle(int l,int b, int h)
//parameterized constructor
{
class Program{
public static void Main()
length=l;
{
breadth=b;
Rectangle r1=new Rectangle(10,15,20);
height=h;
Rectangle r2=new Rectangle(15,25,30);
} Rectangle r3 = new Rectangle();
public void display() r3=r1+r2;
{ r3.display();
Console.WriteLine(length); }
Console.WriteLine(breadth);
Console.WriteLine(height); }
}
26
13
21-03-2024
C# Interface
Interface in C# is a blueprint of a class. It is like abstract class because all the
methods which are declared inside the interface are abstract methods. It
cannot have method body and cannot be instantiated.
27
C# interface example
Let's see the example of interface in C# which has
draw() method. Its implementation is provided by two public class TestInterface
classes: Rectangle and Circle. {
using System;
public static void Main()
public interface Drawable
{ {
void draw(); Rectangle d = new Rectangle();
} d.draw();
public class Rectangle : Drawable Circle c = new Circle();
{ c.draw();
public void draw() }
{ }
Console.WriteLine("drawing rectangle...");
Output:
}
}
public class Circle : Drawable drawing ractangle...
{ drawing circle...
public void draw()
{
Console.WriteLine("drawing circle...");
}
}
28
14
21-03-2024
Collections
Collection classes are specialized classes for data storage and
retrieval. These classes provide support for stacks, queues, lists, and
hash tables. Most collection classes implement the same interfaces.
29
30
15
21-03-2024
Stack
It represents a last-in, first out collection of object. It is used when you need
a last-in, first-out access of items. When you add an item in the list, it is
called pushing the item and when you remove it, it is called popping the
item.
31
32
16
21-03-2024
Queue
It represents a first-in, first out collection of object. It is used when you need
a first-in, first-out access of items. When you add an item in the list, it is
called enqueue, and when you remove an item, it is called deque.
33
using System;
Console.WriteLine();
using System.Collections;
Console.WriteLine("Removing some values ");
char ch = (char)q.Dequeue();
namespace CollectionsApplication { Console.WriteLine("The removed value: {0}", ch);
ch = (char)q.Dequeue();
class Program {
Console.WriteLine("The removed value: {0}", ch);
static void Main(string[] args) {
Queue q = new Queue(); Console.ReadKey();
}
q.Enqueue('A'); }
q.Enqueue('M'); }
q.Enqueue('G'); When the above code is compiled and executed, it produces the
following result −
q.Enqueue('W');
Current queue:
Console.WriteLine("Current queue: "); AM G W
Current queue:
foreach (char c in q) Console.Write(c + " "); AM G WV H
Console.WriteLine(); Removing values
q.Enqueue('V'); The removed value: A
The removed value: M
q.Enqueue('H');
Console.WriteLine("Current queue: ");
foreach (char c in q) Console.Write(c + " ");
34
17
21-03-2024
Delegates
C# delegates are similar to pointers to functions, in C or C++. A delegate is a reference
type variable that holds the reference to a method. The reference can be changed at
runtime.
Delegates are especially used for implementing events and the call-back methods. All
delegates are implicitly derived from the System.Delegate class.
Declaring Delegates
Delegate declaration determines the methods that can be referenced by the delegate.
A delegate can refer to a method, which has the same signature as that of the
delegate.
35
36
18
21-03-2024
Instantiating Delegates
Once a delegate type is declared, a delegate object must be created with the
new keyword and be associated with a particular method.
When creating a delegate, the argument passed to the new expression is written
similar to a method call, but without the arguments to the method. For example
−
37
38
19
21-03-2024
Following example demonstrates declaration, instantiation, and use of a delegate that can
be used to reference methods that take an integer parameter and returns an integer value.
static void Main(string[] args) {
using System;
//create delegate instances
NumberChanger nc1 = new
delegate int NumberChanger(int n);
NumberChanger(AddNum);
namespace DelegateAppl {
NumberChanger nc2 = new
NumberChanger(MultNum);
class TestDelegate {
static int num = 10;
//calling the methods using the delegate objects
nc1(25);
public static int AddNum(int p) {
Console.WriteLine("Value of Num: {0}", getNum());
num += p;
nc2(5);
return num;
Console.WriteLine("Value of Num: {0}", getNum());
}
Console.ReadKey();
public static int MultNum(int q) {
}
num *= q;
}
return num;
}
}
When the above code is compiled and executed, it
public static int getNum() {
produces the following result −
return num;
}
Value of Num: 35
Value of Num: 175
39
20