SlideShare a Scribd company logo
1 of 5
http://www.msdotnet.co.in
Delegate is an object type which can be used to Encapsulate the method
call and handle the event.Through Delegate only that function can be called
whose signature and Return Type is same
as Delegate. Delegate basically helps for calling the other class
method. Delegate can be declared within the class or outside the
class.Delegate is like a method but it use like a class to create the object of
del class.
There are some feature of delegate in C#.
1. Declaration of delegate.
Syntax:
<Access specifier --> delegate keyword--> Return type --> delegate Name --
> (Parameter list)>
Ex: Public delegate void del();
2. Declaration of the delegate Method.
A method which will be called by the delegate is known as delegate method.
Ex:
public class student
{
public void show()
{
Console.WriteLine("Hello Friends");
Console.ReadLine();
}
}
3. Declaration of the delegate object.
Ex;
student obj = new student();
del delobj = new del(obj.show);
4. Call the delegate object.
delobj();
There can be two Types of delegate .
1. Single cast delegate:- In this type of delegate one delegate object can
call only method.
Ex:-
using System;
using System.Text;
namespace @delegate
{
class Program
{
public delegate void del(int p,int q);
public class student
{
public void add(int a,int b)
{
int res = a + b;
Console.WriteLine("addition of number is="+res);
Console.ReadLine();
}
}
public static void Main(string[] args)
{
student obj = new student();
del delobj = new del(obj.add);
delobj(14,15);
}
}
}
Description:- In this above program one delegate Object (delobj) is calling
only one method(show()) of the student class.So this is called single cast
delegate.
2. Multicast delegate:- One delegate object can call more than
one Method in Sequence.
In case of Multicast delegate,Delegate should not have any return type because
after calling the delegate object we will get the return value only by last
method.
Ex:-
using System;
using System.Text;
namespace multidelegate
{
class Program
{
public static int res3;
public delegate void del(int p,int q);
public delegate int del1(int x);
public class student
{
public void add(int a,int b)
{
int res1 = a + b;
Console.WriteLine("addition of number is=" +res1);
Console.ReadLine();
}
public void multi(int m,int n)
{
float res2 = (m * n);
Console.WriteLine("Multiplication of number is=" +res2);
Console.ReadLine();
}
public int saquare(int r)
{
res3 = r * r;
return res3;
}
}
public static void Main(string[] args)
{
student st = new student();
del obj1 = new del(st.add);
obj1 += new del(st.multi);
obj1(12,12);
del1 obj2 = new del1(st.saquare);
obj2(5);
Console.WriteLine("squre of number is=" +res3);
Console.ReadLine();
}
}
}
Description:- In this above example one delegate object(obj1) is calling two
method[add(),multi()] of student class,and another delegate object(obj2) is
calling one method (saquare).so, this example of single and multicast
delegate.
Real Definition of delegate in Our life:-
Suppose there three person in a department.
1. Teacher
2. Peon
3. Tea Maker
In one story,Teacher Ring the well,a peon came.Teacher said for
tea,peon went to Tea maker and ordered for tea.Tea Maker accepted the
order and made the tea.Peon came back with tea.
Now conclusion of this story is favor of Delegate is;
here:-
Teacher-->Event Creator
Ring --> Event
Peon --> Delegate
Tea maker-->Event handler
Anonymous method in context of delegate:-
An anonymous is a collection of method which is directly pass to the
delegate.
OR
Anonymous is a method which does not have any specific name but it is a
collection of statement which is directly pass to the delegate.
EX.
using System;
using System.Text;
namespace anonymous
{
class Program
{
public delegate int del(String s);
static void Main(string[] args)
{
del delobj = delegate(String s)
{
for (int i = 0; i < 5; i++)
{
Console.WriteLine(i);
Console.WriteLine(s);
Console.ReadLine();
}
return 1;
};
int res= delobj("customer");
Console.WriteLine(res);
Console.ReadLine();
}
}
}
Description:-In above example i have directly passed statement to the
delegate.So this called Anonymous method.
Note:-
1. We can declare the delegate outside or inside the class.
2. But we can declare the Event only inside the class.
3. Every event has a specific delegate for handling the event.
I hope this is helpful for you.
Click below for download whole application.
Download
Delegate

More Related Content

What's hot

Class, object and inheritance in python
Class, object and inheritance in pythonClass, object and inheritance in python
Class, object and inheritance in pythonSantosh Verma
 
Introduction to OOP with PHP
Introduction to OOP with PHPIntroduction to OOP with PHP
Introduction to OOP with PHPMichael Peacock
 
Inheritance, polymorphisam, abstract classes and composition)
Inheritance, polymorphisam, abstract classes and composition)Inheritance, polymorphisam, abstract classes and composition)
Inheritance, polymorphisam, abstract classes and composition)farhan amjad
 
Object Oriented Programming with PHP 5 - More OOP
Object Oriented Programming with PHP 5 - More OOPObject Oriented Programming with PHP 5 - More OOP
Object Oriented Programming with PHP 5 - More OOPWildan Maulana
 
Object Oriented Programming in PHP
Object Oriented Programming in PHPObject Oriented Programming in PHP
Object Oriented Programming in PHPLorna Mitchell
 
C++ programming introduction
C++ programming introductionC++ programming introduction
C++ programming introductionsandeep54552
 
Variables in python
Variables in pythonVariables in python
Variables in pythonJaya Kumari
 
Class and Objects in PHP
Class and Objects in PHPClass and Objects in PHP
Class and Objects in PHPRamasubbu .P
 
Classes and objects
Classes and objectsClasses and objects
Classes and objectsAshok Kumar
 
Basics of Object Oriented Programming in Python
Basics of Object Oriented Programming in PythonBasics of Object Oriented Programming in Python
Basics of Object Oriented Programming in PythonSujith Kumar
 
Object Oriented Programming With PHP 5 #2
Object Oriented Programming With PHP 5 #2Object Oriented Programming With PHP 5 #2
Object Oriented Programming With PHP 5 #2Wildan Maulana
 

What's hot (20)

Class, object and inheritance in python
Class, object and inheritance in pythonClass, object and inheritance in python
Class, object and inheritance in python
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Inheritance
InheritanceInheritance
Inheritance
 
C++ oop
C++ oopC++ oop
C++ oop
 
Introduction to OOP with PHP
Introduction to OOP with PHPIntroduction to OOP with PHP
Introduction to OOP with PHP
 
Inheritance, polymorphisam, abstract classes and composition)
Inheritance, polymorphisam, abstract classes and composition)Inheritance, polymorphisam, abstract classes and composition)
Inheritance, polymorphisam, abstract classes and composition)
 
Object Oriented Programming with PHP 5 - More OOP
Object Oriented Programming with PHP 5 - More OOPObject Oriented Programming with PHP 5 - More OOP
Object Oriented Programming with PHP 5 - More OOP
 
Object Oriented Programming in PHP
Object Oriented Programming in PHPObject Oriented Programming in PHP
Object Oriented Programming in PHP
 
Lecture5
Lecture5Lecture5
Lecture5
 
Demystifying oop
Demystifying oopDemystifying oop
Demystifying oop
 
C++ programming introduction
C++ programming introductionC++ programming introduction
C++ programming introduction
 
Java Basic day-1
Java Basic day-1Java Basic day-1
Java Basic day-1
 
Python: Basic Inheritance
Python: Basic InheritancePython: Basic Inheritance
Python: Basic Inheritance
 
Variables in python
Variables in pythonVariables in python
Variables in python
 
OOPS IN C++
OOPS IN C++OOPS IN C++
OOPS IN C++
 
Class and Objects in PHP
Class and Objects in PHPClass and Objects in PHP
Class and Objects in PHP
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
Basics of Object Oriented Programming in Python
Basics of Object Oriented Programming in PythonBasics of Object Oriented Programming in Python
Basics of Object Oriented Programming in Python
 
Object Oriented Programming With PHP 5 #2
Object Oriented Programming With PHP 5 #2Object Oriented Programming With PHP 5 #2
Object Oriented Programming With PHP 5 #2
 
Python programming : Inheritance and polymorphism
Python programming : Inheritance and polymorphismPython programming : Inheritance and polymorphism
Python programming : Inheritance and polymorphism
 

Viewers also liked

Abud modulo 2 actividad 1
Abud modulo 2 actividad 1Abud modulo 2 actividad 1
Abud modulo 2 actividad 1Silvia Abud
 
Business Credibility Packet Linkedin
Business Credibility Packet LinkedinBusiness Credibility Packet Linkedin
Business Credibility Packet LinkedinLeislie Hernandez
 
Informacion electronica
Informacion electronicaInformacion electronica
Informacion electronicaEli Garcia
 
Private lender presentation
Private lender presentationPrivate lender presentation
Private lender presentationtodhunter
 
Pragati Leadership's Credentials - Say hello to India's most trusted L&D partner
Pragati Leadership's Credentials - Say hello to India's most trusted L&D partnerPragati Leadership's Credentials - Say hello to India's most trusted L&D partner
Pragati Leadership's Credentials - Say hello to India's most trusted L&D partnerPragati Leadership
 
Economic system Capitalism Socialism And mixed Economy By Milan Kagarana
Economic system Capitalism Socialism And mixed Economy By Milan KagaranaEconomic system Capitalism Socialism And mixed Economy By Milan Kagarana
Economic system Capitalism Socialism And mixed Economy By Milan KagaranaMilan Kagarana
 

Viewers also liked (11)

Abud modulo 2 actividad 1
Abud modulo 2 actividad 1Abud modulo 2 actividad 1
Abud modulo 2 actividad 1
 
Business Credibility Packet Linkedin
Business Credibility Packet LinkedinBusiness Credibility Packet Linkedin
Business Credibility Packet Linkedin
 
RENE 2.0
RENE 2.0RENE 2.0
RENE 2.0
 
Informacion electronica
Informacion electronicaInformacion electronica
Informacion electronica
 
Private lender presentation
Private lender presentationPrivate lender presentation
Private lender presentation
 
Pragati Leadership's Credentials - Say hello to India's most trusted L&D partner
Pragati Leadership's Credentials - Say hello to India's most trusted L&D partnerPragati Leadership's Credentials - Say hello to India's most trusted L&D partner
Pragati Leadership's Credentials - Say hello to India's most trusted L&D partner
 
Unix signals
Unix signalsUnix signals
Unix signals
 
M saeed CV
M saeed CVM saeed CV
M saeed CV
 
S.s report
S.s reportS.s report
S.s report
 
Cartilla fundamentos
Cartilla fundamentosCartilla fundamentos
Cartilla fundamentos
 
Economic system Capitalism Socialism And mixed Economy By Milan Kagarana
Economic system Capitalism Socialism And mixed Economy By Milan KagaranaEconomic system Capitalism Socialism And mixed Economy By Milan Kagarana
Economic system Capitalism Socialism And mixed Economy By Milan Kagarana
 

Similar to Delegate

Implementation of oop concept in c++
Implementation of oop concept in c++Implementation of oop concept in c++
Implementation of oop concept in c++Swarup Boro
 
Polymorphism.pptx
Polymorphism.pptxPolymorphism.pptx
Polymorphism.pptxVijaykota11
 
PROGRAMMING USING C#.NET SARASWATHI RAMALINGAM
PROGRAMMING USING C#.NET SARASWATHI RAMALINGAMPROGRAMMING USING C#.NET SARASWATHI RAMALINGAM
PROGRAMMING USING C#.NET SARASWATHI RAMALINGAMSaraswathiRamalingam
 
JAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examplesJAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examplesSunil Kumar Gunasekaran
 
Oop features java presentationshow
Oop features java presentationshowOop features java presentationshow
Oop features java presentationshowilias ahmed
 
CJP Unit-1 contd.pptx
CJP Unit-1 contd.pptxCJP Unit-1 contd.pptx
CJP Unit-1 contd.pptxRAJASEKHARV10
 
Demystifying Object-Oriented Programming - ZendCon 2016
Demystifying Object-Oriented Programming - ZendCon 2016Demystifying Object-Oriented Programming - ZendCon 2016
Demystifying Object-Oriented Programming - ZendCon 2016Alena Holligan
 
UNIT_-II_2021R.pptx
UNIT_-II_2021R.pptxUNIT_-II_2021R.pptx
UNIT_-II_2021R.pptxRDeepa9
 
classes & objects.ppt
classes & objects.pptclasses & objects.ppt
classes & objects.pptBArulmozhi
 
Python programming computer science and engineering
Python programming computer science and engineeringPython programming computer science and engineering
Python programming computer science and engineeringIRAH34
 
Demystifying Object-Oriented Programming - PHP UK Conference 2017
Demystifying Object-Oriented Programming - PHP UK Conference 2017Demystifying Object-Oriented Programming - PHP UK Conference 2017
Demystifying Object-Oriented Programming - PHP UK Conference 2017Alena Holligan
 
Classes, objects and methods
Classes, objects and methodsClasses, objects and methods
Classes, objects and methodsfarhan amjad
 

Similar to Delegate (20)

Implementation of oop concept in c++
Implementation of oop concept in c++Implementation of oop concept in c++
Implementation of oop concept in c++
 
Java Basic day-2
Java Basic day-2Java Basic day-2
Java Basic day-2
 
Python_Unit_2 OOPS.pptx
Python_Unit_2  OOPS.pptxPython_Unit_2  OOPS.pptx
Python_Unit_2 OOPS.pptx
 
Polymorphism.pptx
Polymorphism.pptxPolymorphism.pptx
Polymorphism.pptx
 
PROGRAMMING USING C#.NET SARASWATHI RAMALINGAM
PROGRAMMING USING C#.NET SARASWATHI RAMALINGAMPROGRAMMING USING C#.NET SARASWATHI RAMALINGAM
PROGRAMMING USING C#.NET SARASWATHI RAMALINGAM
 
JAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examplesJAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examples
 
Delegates and events
Delegates and events   Delegates and events
Delegates and events
 
Only oop
Only oopOnly oop
Only oop
 
Oop features java presentationshow
Oop features java presentationshowOop features java presentationshow
Oop features java presentationshow
 
Opp concept in c++
Opp concept in c++Opp concept in c++
Opp concept in c++
 
CJP Unit-1 contd.pptx
CJP Unit-1 contd.pptxCJP Unit-1 contd.pptx
CJP Unit-1 contd.pptx
 
Demystifying Object-Oriented Programming - ZendCon 2016
Demystifying Object-Oriented Programming - ZendCon 2016Demystifying Object-Oriented Programming - ZendCon 2016
Demystifying Object-Oriented Programming - ZendCon 2016
 
UNIT_-II_2021R.pptx
UNIT_-II_2021R.pptxUNIT_-II_2021R.pptx
UNIT_-II_2021R.pptx
 
classes & objects.ppt
classes & objects.pptclasses & objects.ppt
classes & objects.ppt
 
Python programming computer science and engineering
Python programming computer science and engineeringPython programming computer science and engineering
Python programming computer science and engineering
 
Demystifying Object-Oriented Programming - PHP UK Conference 2017
Demystifying Object-Oriented Programming - PHP UK Conference 2017Demystifying Object-Oriented Programming - PHP UK Conference 2017
Demystifying Object-Oriented Programming - PHP UK Conference 2017
 
Inheritance
InheritanceInheritance
Inheritance
 
.NET F# Events
.NET F# Events.NET F# Events
.NET F# Events
 
Class and object in C++ By Pawan Thakur
Class and object in C++ By Pawan ThakurClass and object in C++ By Pawan Thakur
Class and object in C++ By Pawan Thakur
 
Classes, objects and methods
Classes, objects and methodsClasses, objects and methods
Classes, objects and methods
 

Recently uploaded

Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the ClassroomPooky Knightsmith
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxDr. Sarita Anand
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jisc
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfDr Vijay Vishwakarma
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17Celine George
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxmarlenawright1
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...Amil baba
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxJisc
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Pooja Bhuva
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structuredhanjurrannsibayan2
 

Recently uploaded (20)

Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 

Delegate

  • 1. http://www.msdotnet.co.in Delegate is an object type which can be used to Encapsulate the method call and handle the event.Through Delegate only that function can be called whose signature and Return Type is same as Delegate. Delegate basically helps for calling the other class method. Delegate can be declared within the class or outside the class.Delegate is like a method but it use like a class to create the object of del class. There are some feature of delegate in C#. 1. Declaration of delegate. Syntax: <Access specifier --> delegate keyword--> Return type --> delegate Name -- > (Parameter list)> Ex: Public delegate void del(); 2. Declaration of the delegate Method. A method which will be called by the delegate is known as delegate method. Ex: public class student { public void show() { Console.WriteLine("Hello Friends"); Console.ReadLine(); } } 3. Declaration of the delegate object. Ex; student obj = new student(); del delobj = new del(obj.show); 4. Call the delegate object. delobj(); There can be two Types of delegate . 1. Single cast delegate:- In this type of delegate one delegate object can call only method. Ex:- using System;
  • 2. using System.Text; namespace @delegate { class Program { public delegate void del(int p,int q); public class student { public void add(int a,int b) { int res = a + b; Console.WriteLine("addition of number is="+res); Console.ReadLine(); } } public static void Main(string[] args) { student obj = new student(); del delobj = new del(obj.add); delobj(14,15); } } } Description:- In this above program one delegate Object (delobj) is calling only one method(show()) of the student class.So this is called single cast delegate. 2. Multicast delegate:- One delegate object can call more than one Method in Sequence. In case of Multicast delegate,Delegate should not have any return type because after calling the delegate object we will get the return value only by last method. Ex:- using System; using System.Text; namespace multidelegate { class Program { public static int res3; public delegate void del(int p,int q); public delegate int del1(int x); public class student { public void add(int a,int b) { int res1 = a + b; Console.WriteLine("addition of number is=" +res1);
  • 3. Console.ReadLine(); } public void multi(int m,int n) { float res2 = (m * n); Console.WriteLine("Multiplication of number is=" +res2); Console.ReadLine(); } public int saquare(int r) { res3 = r * r; return res3; } } public static void Main(string[] args) { student st = new student(); del obj1 = new del(st.add); obj1 += new del(st.multi); obj1(12,12); del1 obj2 = new del1(st.saquare); obj2(5); Console.WriteLine("squre of number is=" +res3); Console.ReadLine(); } } } Description:- In this above example one delegate object(obj1) is calling two method[add(),multi()] of student class,and another delegate object(obj2) is calling one method (saquare).so, this example of single and multicast delegate. Real Definition of delegate in Our life:- Suppose there three person in a department. 1. Teacher 2. Peon 3. Tea Maker In one story,Teacher Ring the well,a peon came.Teacher said for tea,peon went to Tea maker and ordered for tea.Tea Maker accepted the order and made the tea.Peon came back with tea. Now conclusion of this story is favor of Delegate is; here:-
  • 4. Teacher-->Event Creator Ring --> Event Peon --> Delegate Tea maker-->Event handler Anonymous method in context of delegate:- An anonymous is a collection of method which is directly pass to the delegate. OR Anonymous is a method which does not have any specific name but it is a collection of statement which is directly pass to the delegate. EX. using System; using System.Text; namespace anonymous { class Program { public delegate int del(String s); static void Main(string[] args) { del delobj = delegate(String s) { for (int i = 0; i < 5; i++) { Console.WriteLine(i); Console.WriteLine(s); Console.ReadLine(); } return 1; }; int res= delobj("customer"); Console.WriteLine(res); Console.ReadLine(); } } } Description:-In above example i have directly passed statement to the delegate.So this called Anonymous method. Note:- 1. We can declare the delegate outside or inside the class. 2. But we can declare the Event only inside the class. 3. Every event has a specific delegate for handling the event. I hope this is helpful for you. Click below for download whole application. Download