SlideShare una empresa de Scribd logo
1 de 19
C# .Net Delegates and Events
C# .Net Delegates and Events

•   C# .Net Delegates and Events
•   Delegate is one of the base types in .NET. Delegate is a class, which is used to create delegate
    at runtime.
•   Delegate in C# is similar to a function pointer in C or C++. It's a new type of object in C#.
    Delegate is very special type of object as earlier the entire the object we used to defined
    contained data but delegate just contains the details of a method.
•   Need of delegate
•   There might be situation in which you want to pass methods around to other methods. For
    this purpose we create delegate.
•   A delegate is a class that encapsulates a method signature. Although it can be used in any
    context, it often serves as the basis for the event-handling model in C# but can be used in a
    context removed from event handling (e.g. passing a method to a method through a delegate
    parameter).
•   One good way of understanding delegates is by thinking of a delegate as something that
    gives a name to a method signature.
•   Example:
C# .Net Delegates and Events

•
    public delegate int DelegateMethod(int x, int y);

•
    Any method that matches the delegate's signature, which consists of the return type and
    parameters, can be assigned to the delegate. This makes is possible to programmatically change
    method calls, and also plug new code into existing classes. As long as you know the delegate's
    signature, you can assign your own-delegated method.
•   This ability to refer to a method as a parameter makes delegates ideal for defining callback
    methods.
•   Delegate magic
•   In class we create its object, which is instance, but in delegate when we create instance that is also
    referred as delegate (means whatever you do you will get delegate).
•   Delegate does not know or care about the class of the object that it references. Any object will do;
    all that matters is that the method's argument types and return type match the delegate's. This
    makes delegates perfectly suited for "anonymous" invocation.
•   Benefit of delegates
•   In simple words delegates are object oriented and type-safe and very secure as they ensure that
    the signature of the method being called is correct. Delegate helps in code optimization.
C# .Net Delegates and Events

•   Types of delegates
•   1) Singlecast delegates
•   2) Multiplecast delegates
•   Delegate is a class. Any delegate is inherited from base delegate class of .NET class
    library when it is declared. This can be from either of the two classes from
    System.Delegate or System.MulticastDelegate.
•   Singlecast delegate
•   Singlecast delegate point to single method at a time. In this the delegate is
    assigned to a single method at a time. They are derived from System.Delegate
    class.
•   Multicast Delegate
•   When a delegate is wrapped with more than one method that is known as a
    multicast delegate.
•
    In C#, delegates are multicast, which means that they can point to more than one
    function at a time. They are derived from System.MulticastDelegate class.
C# .Net Delegates and Events

•   There are three steps in defining and using delegates:
•   1. Declaration
•   To create a delegate, you use the delegate keyword.
•   [attributes] [modifiers] delegate ReturnType Name ([formal-parameters]);
•   The attributes factor can be a normal C# attribute.
•   The modifier can be one or an appropriate combination of the following keywords:
    new, public, private, protected, or internal.
•   The ReturnType can be any of the data types we have used so far. It can also be a
    type void or the name of a class.
•   The Name must be a valid C# name.
•   Because a delegate is some type of a template for a method, you must use
    parentheses, required for every method. If this method will not take any
    argument, leave the parentheses empty.
•   Example:
•   public delegate void DelegateExample();
•   The code piece defines a delegate DelegateExample() that has void return type
    and accept no parameters.
C# .Net Delegates and Events

•   2. Instantiation
•   DelegateExample d1 = new DelegateExample(Display);
•   The above code piece show how the delegate is initiated
•   3. Invocation
•   d1();
•   The above code piece invoke the delegate d1().
•   Program to demonstrate Singlecast delegate
•   using System;
•   namespace ConsoleApplication5
•   {
•   class Program
•   {
•   public delegate void delmethod();
•   public class P
•   {
•   public static void display()
•   {
•   Console.WriteLine("Hello!");
•   }
•   public static void show()
•   {
•   Console.WriteLine("Hi!");
•   }
•
C# .Net Delegates and Events
public void print()
{
Console.WriteLine("Print");
}}
static void Main(string[] args)
{
// here we have assigned static method show() of class P to delegate delmethod()
delmethod del1 = P.show;
// here we have assigned static method display() of class P to delegate delmethod() using new operator
// you can use both ways to assign the delagate
delmethod del2 = new delmethod(P.display);
P obj = new P();
// here first we have create instance of class P and assigned the method print() to the delegate i.e. delegate
with class
delmethod del3 = obj.print;
del1();
del2();
del3();
Console.ReadLine();
} }}
C# .Net Delegates and Events
Program to demonstrate Multicast delegate
using System;
namespace delegate_Example4
{
class Program
{
public delegate void delmethod(int x, int y);
public class TestMultipleDelegate
{
public void plus_Method1(int x, int y)
{
Console.Write("You are in plus_Method");
Console.WriteLine(x + y);
}
public void subtract_Method2(int x, int y)
{
Console.Write("You are in subtract_Method");
Console.WriteLine(x - y);
}}
C# .Net Delegates and Events

static void Main(string[] args)
{
TestMultipleDelegate obj = new TestMultipleDelegate();
delmethod del = new delmethod(obj.plus_Method1);
// Here we have multicast
del += new delmethod(obj.subtract_Method2);
// plus_Method1 and subtract_Method2 are called
del(50, 10);
Console.WriteLine();
//Here again we have multicast
del -= new delmethod(obj.plus_Method1);
//Only subtract_Method2 is called
del(20, 10);
Console.ReadLine();
}
}
}
•
C# .Net Delegates and Events

• Point to remember about Delegates:
• Delegates are similar to C++ function pointers, but are type
  safe.
• Delegate gives a name to a method signature.
• Delegates allow methods to be passed as parameters.
• Delegates can be used to define callback methods.
• Delegates can be chained together; for example, multiple
  methods can be called on a single event.
• C# version 2.0 introduces the concept of Anonymous
  Methods, which permit code blocks to be passed as
  parameters in place of a separately defined method.
• Delegate helps in code optimization.
C# .Net Delegates and Events
Usage areas of delegates
The most common example of using delegates is in events.
They are extensively used in threading
Delegates are also used for generic class libraries, which have generic functionality, defined.
An Anonymous Delegate
You can create a delegate, but there is no need to declare the method associated with it. You do not have to
explicitly define a method prior to using the delegate. Such a method is referred to as anonymous.
In other words, if a delegate itself contains its method definition it is known as anonymous method.
Program to show An Anonymous Delegate
using System;
public delegate void Test();
public class Program
{
static int Main()
{
Test Display = delegate()
{
Console.WriteLine("Anonymous Delegate method"); };
Display();
return 0;
} }
C# .Net Delegates and Events
•   Events
•   Event and delegate are linked together.
•   Event is a reference of delegate i.e. when event will be raised delegate will be called.
•   In C# terms, events are a special form of delegate. Events are nothing but change of state. Events
    play an important part in GUI programming. Events and delegates work hand-in-hand to provide a
    program's functionality.
•   A C# event is a class member that is activated whenever the event it was designed for occurs.
•   It starts with a class that declares an event. Any class, including the same class that the event is
    declared in, may register one of its methods for the event. This occurs through a delegate, which
    specifies the signature of the method that is registered for the event. The event keyword is a
    delegate modifier. It must always be used in connection with a delegate.
•   The delegate may be one of the pre-defined .NET delegates or one you declare yourself. Whichever
    is appropriate, you assign the delegate to the event, which effectively registers the method that will
    be called when the event fires.
•   How to use events?
•   Once an event is declared, it must be associated with one or more event handlers before it can be
    raised. An event handler is nothing but a method that is called using a delegate. Use the +=
    operator to associate an event with an instance of a delegate that already exists.
•   Example:
•   obj.MyEvent += new MyDelegate(obj.Display);
•   An event has the value null if it has no registered listeners.
•   Although events are mostly used in Windows controls programming, they can also be implemented
    in console, web and other applications.
C# .Net Delegates and Events
Program for creating custom Singlecast delegate and event
using System;
namespace delegate_custom
{
class Program
{
public delegate void MyDelegate(int a);
public class XX
{
public event MyDelegate MyEvent;
public void RaiseEvent()
{
MyEvent(20);
Console.WriteLine("Event Raised");
}
public void Display(int x)
{
Console.WriteLine("Display Method {0}", x);
}
}
C# .Net Delegates and Events

static void Main(string[] args)
{
XX obj = new XX();
obj.MyEvent += new MyDelegate(obj.Display);
obj.RaiseEvent();
Console.ReadLine();
}
}
}
C# .Net Delegates and Events
Program for creating custom Multiplecast delegate and event
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace delegate_custom_multicast
{
class Program
{
public delegate void MyDelegate(int a, int b);
public class XX
{
public event MyDelegate MyEvent;
public void RaiseEvent(int a, int b)
{
MyEvent(a, b);
Console.WriteLine("Event Raised");
}
C# .Net Delegates and Events
public void Add(int x, int y)
{
Console.WriteLine("Add Method {0}", x + y);
}
public void Subtract(int x, int y)
{
Console.WriteLine("Subtract Method {0}", x - y);
}}
static void Main(string[] args)
{
XX obj = new XX();
obj.MyEvent += new MyDelegate(obj.Add);
obj.MyEvent += new MyDelegate(obj.Subtract);
obj.RaiseEvent(20, 10);
Console.ReadLine();
}}}
C# .Net Delegates and Events
• Practical Anonymous Method using delegate and events

using System.Text;
using System.Windows.Forms;
namespace delegate_anonymous
{
public partial class Form1 : Form
{
public delegate int MyDelegate(int a, int b);
EventHandler d1 = delegate(object sender, EventArgs e)
{
MessageBox.Show("Anonymous Method");
};
MyDelegate d2= delegate (int a, int b)
{
return(a+b); };
public Form1()
{
InitializeComponent();
}
•
C# .Net Delegates and Events

private void button1_Click(object sender, EventArgs e)
{
d1(sender, e);
}
private void button2_Click(object sender, EventArgs e)
{
int i = d2(10, 20);
MessageBox.Show(i.ToString());
}
}
}

Más contenido relacionado

La actualidad más candente

La actualidad más candente (20)

Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
 
C# Delegates and Event Handling
C# Delegates and Event HandlingC# Delegates and Event Handling
C# Delegates and Event Handling
 
Operators in java presentation
Operators in java presentationOperators in java presentation
Operators in java presentation
 
Constructor ppt
Constructor pptConstructor ppt
Constructor ppt
 
C# Access modifiers
C# Access modifiersC# Access modifiers
C# Access modifiers
 
Introduction to method overloading & method overriding in java hdm
Introduction to method overloading & method overriding  in java  hdmIntroduction to method overloading & method overriding  in java  hdm
Introduction to method overloading & method overriding in java hdm
 
Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)
 
Java constructors
Java constructorsJava constructors
Java constructors
 
Functions in javascript
Functions in javascriptFunctions in javascript
Functions in javascript
 
Polymorphism presentation in java
Polymorphism presentation in javaPolymorphism presentation in java
Polymorphism presentation in java
 
6. static keyword
6. static keyword6. static keyword
6. static keyword
 
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteChapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
 
07. Virtual Functions
07. Virtual Functions07. Virtual Functions
07. Virtual Functions
 
Function overloading ppt
Function overloading pptFunction overloading ppt
Function overloading ppt
 
Delegetes in c#
Delegetes in c#Delegetes in c#
Delegetes in c#
 
OOP java
OOP javaOOP java
OOP java
 
Jsp ppt
Jsp pptJsp ppt
Jsp ppt
 
Wrapper classes
Wrapper classes Wrapper classes
Wrapper classes
 
C# Delegates
C# DelegatesC# Delegates
C# Delegates
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
 

Similar a Delegates and events

Delegates and events
Delegates and eventsDelegates and events
Delegates and eventsIblesoft
 
PROGRAMMING USING C#.NET SARASWATHI RAMALINGAM
PROGRAMMING USING C#.NET SARASWATHI RAMALINGAMPROGRAMMING USING C#.NET SARASWATHI RAMALINGAM
PROGRAMMING USING C#.NET SARASWATHI RAMALINGAMSaraswathiRamalingam
 
Review of c_sharp2_features_part_ii
Review of c_sharp2_features_part_iiReview of c_sharp2_features_part_ii
Review of c_sharp2_features_part_iiNico Ludwig
 
Presentation 4th
Presentation 4thPresentation 4th
Presentation 4thConnex
 
Introduction to objective c
Introduction to objective cIntroduction to objective c
Introduction to objective cSunny Shaikh
 
Advanced programming topics asma
Advanced programming topics asmaAdvanced programming topics asma
Advanced programming topics asmaAbdullahJana
 
Day02 01 Advance Feature in C# DH TDT
Day02 01 Advance Feature in C# DH TDTDay02 01 Advance Feature in C# DH TDT
Day02 01 Advance Feature in C# DH TDTNguyen Patrick
 
PATTERNS06 - The .NET Event Model
PATTERNS06 - The .NET Event ModelPATTERNS06 - The .NET Event Model
PATTERNS06 - The .NET Event ModelMichael Heron
 
I assignmnt(oops)
I assignmnt(oops)I assignmnt(oops)
I assignmnt(oops)Jay Patel
 
Java-Intro.pptx
Java-Intro.pptxJava-Intro.pptx
Java-Intro.pptxVijalJain3
 
Presentation 1st
Presentation 1stPresentation 1st
Presentation 1stConnex
 
Can't Dance The Lambda
Can't Dance The LambdaCan't Dance The Lambda
Can't Dance The LambdaTogakangaroo
 
Refactoring Tips by Martin Fowler
Refactoring Tips by Martin FowlerRefactoring Tips by Martin Fowler
Refactoring Tips by Martin FowlerIgor Crvenov
 

Similar a Delegates and events (20)

30csharp
30csharp30csharp
30csharp
 
30c
30c30c
30c
 
Delegates and events
Delegates and eventsDelegates and events
Delegates and events
 
PROGRAMMING USING C#.NET SARASWATHI RAMALINGAM
PROGRAMMING USING C#.NET SARASWATHI RAMALINGAMPROGRAMMING USING C#.NET SARASWATHI RAMALINGAM
PROGRAMMING USING C#.NET SARASWATHI RAMALINGAM
 
Review of c_sharp2_features_part_ii
Review of c_sharp2_features_part_iiReview of c_sharp2_features_part_ii
Review of c_sharp2_features_part_ii
 
Presentation 4th
Presentation 4thPresentation 4th
Presentation 4th
 
Introduction to objective c
Introduction to objective cIntroduction to objective c
Introduction to objective c
 
OOPJ.pptx
OOPJ.pptxOOPJ.pptx
OOPJ.pptx
 
Advanced programming topics asma
Advanced programming topics asmaAdvanced programming topics asma
Advanced programming topics asma
 
Interface
InterfaceInterface
Interface
 
Day02 01 Advance Feature in C# DH TDT
Day02 01 Advance Feature in C# DH TDTDay02 01 Advance Feature in C# DH TDT
Day02 01 Advance Feature in C# DH TDT
 
UNIT1-JAVA.pptx
UNIT1-JAVA.pptxUNIT1-JAVA.pptx
UNIT1-JAVA.pptx
 
PATTERNS06 - The .NET Event Model
PATTERNS06 - The .NET Event ModelPATTERNS06 - The .NET Event Model
PATTERNS06 - The .NET Event Model
 
I assignmnt(oops)
I assignmnt(oops)I assignmnt(oops)
I assignmnt(oops)
 
Java-Intro.pptx
Java-Intro.pptxJava-Intro.pptx
Java-Intro.pptx
 
Presentation 1st
Presentation 1stPresentation 1st
Presentation 1st
 
Object Oriented Programming
Object Oriented ProgrammingObject Oriented Programming
Object Oriented Programming
 
Oops ppt
Oops pptOops ppt
Oops ppt
 
Can't Dance The Lambda
Can't Dance The LambdaCan't Dance The Lambda
Can't Dance The Lambda
 
Refactoring Tips by Martin Fowler
Refactoring Tips by Martin FowlerRefactoring Tips by Martin Fowler
Refactoring Tips by Martin Fowler
 

Último

ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomnelietumpap1
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxChelloAnnAsuncion2
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSJoshuaGantuangco2
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
Q4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxQ4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxnelietumpap1
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parentsnavabharathschool99
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxDr.Ibrahim Hassaan
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxMaryGraceBautista27
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...Postal Advocate Inc.
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 

Último (20)

Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptx
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choom
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
Q4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxQ4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptx
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parents
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptx
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptx
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 

Delegates and events

  • 1. C# .Net Delegates and Events
  • 2. C# .Net Delegates and Events • C# .Net Delegates and Events • Delegate is one of the base types in .NET. Delegate is a class, which is used to create delegate at runtime. • Delegate in C# is similar to a function pointer in C or C++. It's a new type of object in C#. Delegate is very special type of object as earlier the entire the object we used to defined contained data but delegate just contains the details of a method. • Need of delegate • There might be situation in which you want to pass methods around to other methods. For this purpose we create delegate. • A delegate is a class that encapsulates a method signature. Although it can be used in any context, it often serves as the basis for the event-handling model in C# but can be used in a context removed from event handling (e.g. passing a method to a method through a delegate parameter). • One good way of understanding delegates is by thinking of a delegate as something that gives a name to a method signature. • Example:
  • 3. C# .Net Delegates and Events • public delegate int DelegateMethod(int x, int y); • Any method that matches the delegate's signature, which consists of the return type and parameters, can be assigned to the delegate. This makes is possible to programmatically change method calls, and also plug new code into existing classes. As long as you know the delegate's signature, you can assign your own-delegated method. • This ability to refer to a method as a parameter makes delegates ideal for defining callback methods. • Delegate magic • In class we create its object, which is instance, but in delegate when we create instance that is also referred as delegate (means whatever you do you will get delegate). • Delegate does not know or care about the class of the object that it references. Any object will do; all that matters is that the method's argument types and return type match the delegate's. This makes delegates perfectly suited for "anonymous" invocation. • Benefit of delegates • In simple words delegates are object oriented and type-safe and very secure as they ensure that the signature of the method being called is correct. Delegate helps in code optimization.
  • 4.
  • 5. C# .Net Delegates and Events • Types of delegates • 1) Singlecast delegates • 2) Multiplecast delegates • Delegate is a class. Any delegate is inherited from base delegate class of .NET class library when it is declared. This can be from either of the two classes from System.Delegate or System.MulticastDelegate. • Singlecast delegate • Singlecast delegate point to single method at a time. In this the delegate is assigned to a single method at a time. They are derived from System.Delegate class. • Multicast Delegate • When a delegate is wrapped with more than one method that is known as a multicast delegate. • In C#, delegates are multicast, which means that they can point to more than one function at a time. They are derived from System.MulticastDelegate class.
  • 6. C# .Net Delegates and Events • There are three steps in defining and using delegates: • 1. Declaration • To create a delegate, you use the delegate keyword. • [attributes] [modifiers] delegate ReturnType Name ([formal-parameters]); • The attributes factor can be a normal C# attribute. • The modifier can be one or an appropriate combination of the following keywords: new, public, private, protected, or internal. • The ReturnType can be any of the data types we have used so far. It can also be a type void or the name of a class. • The Name must be a valid C# name. • Because a delegate is some type of a template for a method, you must use parentheses, required for every method. If this method will not take any argument, leave the parentheses empty. • Example: • public delegate void DelegateExample(); • The code piece defines a delegate DelegateExample() that has void return type and accept no parameters.
  • 7. C# .Net Delegates and Events • 2. Instantiation • DelegateExample d1 = new DelegateExample(Display); • The above code piece show how the delegate is initiated • 3. Invocation • d1(); • The above code piece invoke the delegate d1(). • Program to demonstrate Singlecast delegate • using System; • namespace ConsoleApplication5 • { • class Program • { • public delegate void delmethod(); • public class P • { • public static void display() • { • Console.WriteLine("Hello!"); • } • public static void show() • { • Console.WriteLine("Hi!"); • } •
  • 8. C# .Net Delegates and Events public void print() { Console.WriteLine("Print"); }} static void Main(string[] args) { // here we have assigned static method show() of class P to delegate delmethod() delmethod del1 = P.show; // here we have assigned static method display() of class P to delegate delmethod() using new operator // you can use both ways to assign the delagate delmethod del2 = new delmethod(P.display); P obj = new P(); // here first we have create instance of class P and assigned the method print() to the delegate i.e. delegate with class delmethod del3 = obj.print; del1(); del2(); del3(); Console.ReadLine(); } }}
  • 9. C# .Net Delegates and Events Program to demonstrate Multicast delegate using System; namespace delegate_Example4 { class Program { public delegate void delmethod(int x, int y); public class TestMultipleDelegate { public void plus_Method1(int x, int y) { Console.Write("You are in plus_Method"); Console.WriteLine(x + y); } public void subtract_Method2(int x, int y) { Console.Write("You are in subtract_Method"); Console.WriteLine(x - y); }}
  • 10. C# .Net Delegates and Events static void Main(string[] args) { TestMultipleDelegate obj = new TestMultipleDelegate(); delmethod del = new delmethod(obj.plus_Method1); // Here we have multicast del += new delmethod(obj.subtract_Method2); // plus_Method1 and subtract_Method2 are called del(50, 10); Console.WriteLine(); //Here again we have multicast del -= new delmethod(obj.plus_Method1); //Only subtract_Method2 is called del(20, 10); Console.ReadLine(); } } } •
  • 11. C# .Net Delegates and Events • Point to remember about Delegates: • Delegates are similar to C++ function pointers, but are type safe. • Delegate gives a name to a method signature. • Delegates allow methods to be passed as parameters. • Delegates can be used to define callback methods. • Delegates can be chained together; for example, multiple methods can be called on a single event. • C# version 2.0 introduces the concept of Anonymous Methods, which permit code blocks to be passed as parameters in place of a separately defined method. • Delegate helps in code optimization.
  • 12. C# .Net Delegates and Events Usage areas of delegates The most common example of using delegates is in events. They are extensively used in threading Delegates are also used for generic class libraries, which have generic functionality, defined. An Anonymous Delegate You can create a delegate, but there is no need to declare the method associated with it. You do not have to explicitly define a method prior to using the delegate. Such a method is referred to as anonymous. In other words, if a delegate itself contains its method definition it is known as anonymous method. Program to show An Anonymous Delegate using System; public delegate void Test(); public class Program { static int Main() { Test Display = delegate() { Console.WriteLine("Anonymous Delegate method"); }; Display(); return 0; } }
  • 13. C# .Net Delegates and Events • Events • Event and delegate are linked together. • Event is a reference of delegate i.e. when event will be raised delegate will be called. • In C# terms, events are a special form of delegate. Events are nothing but change of state. Events play an important part in GUI programming. Events and delegates work hand-in-hand to provide a program's functionality. • A C# event is a class member that is activated whenever the event it was designed for occurs. • It starts with a class that declares an event. Any class, including the same class that the event is declared in, may register one of its methods for the event. This occurs through a delegate, which specifies the signature of the method that is registered for the event. The event keyword is a delegate modifier. It must always be used in connection with a delegate. • The delegate may be one of the pre-defined .NET delegates or one you declare yourself. Whichever is appropriate, you assign the delegate to the event, which effectively registers the method that will be called when the event fires. • How to use events? • Once an event is declared, it must be associated with one or more event handlers before it can be raised. An event handler is nothing but a method that is called using a delegate. Use the += operator to associate an event with an instance of a delegate that already exists. • Example: • obj.MyEvent += new MyDelegate(obj.Display); • An event has the value null if it has no registered listeners. • Although events are mostly used in Windows controls programming, they can also be implemented in console, web and other applications.
  • 14. C# .Net Delegates and Events Program for creating custom Singlecast delegate and event using System; namespace delegate_custom { class Program { public delegate void MyDelegate(int a); public class XX { public event MyDelegate MyEvent; public void RaiseEvent() { MyEvent(20); Console.WriteLine("Event Raised"); } public void Display(int x) { Console.WriteLine("Display Method {0}", x); } }
  • 15. C# .Net Delegates and Events static void Main(string[] args) { XX obj = new XX(); obj.MyEvent += new MyDelegate(obj.Display); obj.RaiseEvent(); Console.ReadLine(); } } }
  • 16. C# .Net Delegates and Events Program for creating custom Multiplecast delegate and event using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace delegate_custom_multicast { class Program { public delegate void MyDelegate(int a, int b); public class XX { public event MyDelegate MyEvent; public void RaiseEvent(int a, int b) { MyEvent(a, b); Console.WriteLine("Event Raised"); }
  • 17. C# .Net Delegates and Events public void Add(int x, int y) { Console.WriteLine("Add Method {0}", x + y); } public void Subtract(int x, int y) { Console.WriteLine("Subtract Method {0}", x - y); }} static void Main(string[] args) { XX obj = new XX(); obj.MyEvent += new MyDelegate(obj.Add); obj.MyEvent += new MyDelegate(obj.Subtract); obj.RaiseEvent(20, 10); Console.ReadLine(); }}}
  • 18. C# .Net Delegates and Events • Practical Anonymous Method using delegate and events using System.Text; using System.Windows.Forms; namespace delegate_anonymous { public partial class Form1 : Form { public delegate int MyDelegate(int a, int b); EventHandler d1 = delegate(object sender, EventArgs e) { MessageBox.Show("Anonymous Method"); }; MyDelegate d2= delegate (int a, int b) { return(a+b); }; public Form1() { InitializeComponent(); } •
  • 19. C# .Net Delegates and Events private void button1_Click(object sender, EventArgs e) { d1(sender, e); } private void button2_Click(object sender, EventArgs e) { int i = d2(10, 20); MessageBox.Show(i.ToString()); } } }