SlideShare uma empresa Scribd logo
1 de 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

Mais conteúdo relacionado

Mais procurados

Introduction to OOP with PHP
Introduction to OOP with PHPIntroduction to OOP with PHP
Introduction to OOP with PHP
Michael Peacock
 
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
Wildan Maulana
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
Ashok 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 #2
Wildan Maulana
 

Mais procurados (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
 

Destaque

Business Credibility Packet Linkedin
Business Credibility Packet LinkedinBusiness Credibility Packet Linkedin
Business Credibility Packet Linkedin
Leislie Hernandez
 

Destaque (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
 

Semelhante a Delegate

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
Sunil Kumar Gunasekaran
 
Python programming computer science and engineering
Python programming computer science and engineeringPython programming computer science and engineering
Python programming computer science and engineering
IRAH34
 
Classes, objects and methods
Classes, objects and methodsClasses, objects and methods
Classes, objects and methods
farhan amjad
 

Semelhante a 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
 

Último

Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
negromaestrong
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
PECB
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
MateoGardella
 

Último (20)

Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
 
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
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.
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 

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