Feb 11, 2010

Delegates in C# - Simple Explanation

Delegates
=========

- also referred to as a type safe function pointer
- Improves the performance an application
- Used to call a method asynchronously
- You should follow the same syntax as in the method
- Example on multicast delegates:
delegate void Delegate_Multicast(int x, int y);
Class Class2
{
static void Method1(int x, int y)
{
Console.WriteLine("You r in Method 1");
}

static void Method2(int x, int y)
{
Console.WriteLine("You r in Method 2");
}

public static void "on" />Main()
{
Delegate_Multicast func = new Delegate_Multicast(Method1);
func += new Delegate_Multicast(Method2);
func(1,2); // Method1 and Method2 are called
func -= new Delegate_Multicast(Method1);
func(2,3); // Only Method2 is called
}
}

1 comment:

  1. Events and Delegates
    ===================

    - There are two types of events - Event Source and Event receiver
    - Event source raises an event and Event receiver receives the event. The communication channel between an event source and

    an event receiver is the delegate
    - Two types of delegates - Single cast delegate(System.Delegate) and Multicast delegate(System.MultiCastDelegate)
    - Single Cast Delegate
    Syntax:
    public delegate Boolean delegateName(parm1,parm2)
    Example:
    public delegate Boolean MyDelegate(object senderobj, Int32 x)

    public class Exdelegate
    {
    Boolean MyFunction(object sender,Int32 x)
    {
    return true;
    }
    public static void Main(String args[])
    {
    MyDelegate mdg = new MyDelegate(MyFunction);
    Boolean f = mdg(this,1);
    }
    }
    -Multi Cast Delegate
    Example:
    delegate void Delegate_Multicast(int x, int y);
    Class Class2
    {
    static void Method1(int x, int y)
    {
    Console.WriteLine("You r in Method 1");
    }

    static void Method2(int x, int y)
    {
    Console.WriteLine("You r in Method 2");
    }

    public static void "on" />Main()
    {
    Delegate_Multicast func = new Delegate_Multicast(Method1);
    func += new Delegate_Multicast(Method2);
    func(1,2); // Method1 and Method2 are called
    func -= new Delegate_Multicast(Method1);
    func(2,3); // Only Method2 is called
    }
    }

    FAQ's
    =====
    What are delegates?

    Delegates are similar to function pointers.
    Delegates are used to call a method without having to know at compile time(used to call a method asynchronously).
    Delegates act as an intermediary between an event source and an event destination.


    Are delegates really type-safe?

    Yes.

    What is type safety all about?

    Type safe code can access only the memory locations that it has permission to execute.
    Type safe code can never access any private members of an object.
    Type safe code ensures that objects are isolated from each other
    CLR performs a mandatory type safety check, called verification, during JIT compilation. This is done by a tool called

    peverify.exe
    Ex:-
    C:\Test\bin\Debug>peverify Test.exe

    What other differences exist between delegates and interfaces?


    What are Multi-Cast Delegates?
    multicast delegate which holds more than one method

    What’s the difference between a Delegate and a MultiCastDelegate?

    What’s the difference between a delegate and an event?

    Deligate is a Function Pointer that stores address of SubProgramme
    Events are raised by using Deligates

    Register delegate- an example of adding an InforMessage handler.

    ReplyDelete