Wednesday, June 19, 2013

Delegates in C#

Delegates are pointer toward a function or method.It solved the decoupling problem.

For exampling : when you go to marriage the representative welcomes you and tells someone (References) to look after you. Due to this he easily manage all the guest.

Wouldn’t it be nice to have a delegate that could refer to more than one function at once and invoke them
simultaneously? This would allow the client application to have multiple “listeners” and notify the listeners all
at once when something happens.
In fact, delegates do have this functionality, but you’re more likely to see it in use with .NET events.
Events, which are described in the next chapter, are based on delegates but work at a slightly higher level.
In a typical ASP.NET program, you’ll use events extensively, but you’ll probably never work directly with
delegates.


There are four step to create delegates :-
  1. Declare
  2.  Create
  3. Pointer
  4. Invoke
   public delegate int Pointertoaddfun(int i, int y);
    private int add(int x,int y)
    {
        return x + y;
    }
    private int sub(int x, int y)
    {
        return x + y;
    }
 
    public Pointertoaddfun getpointer(int dir)
    {
        Pointertoaddfun objpoiter=null;
        if (dir == 1)
        { objpoiter = add; }
        if (dir == 2)
        { objpoiter = sub; }
    }

    To watch video : -http://www.youtube.com/watch?v=3ckXHImXMiU

No comments:

Post a Comment