Monday, January 27, 2014

Delegates and Delegates Default Functions

1. Delegates are objects that contain information about the function rather than data.
2. We can encapsulate a reference to the method inside the delegate object.
3. Delegates are type safe.
4. The return type and signature of  delegates should be the same as the function
5.It is not necessary to use delegates with parameters; we can use delegates with parameters and without       parameters.
6.We can use static or nonstatic functions; in the case of static functions we encapsulate the function with the class name and in the case of a nonstatic function we first make the object of the class and  then we use a function with the object.

Example:
namespace Delegates
 {
    public delegate int mydelegate(int a,int b);
     class Program
     {
         static void Main(string[] args)
         {
             mydelegate delObj = new mydelegate(abc.Sub);
             abc delObj = new abc();
             mydelegate delObj1= new mydelegate(obj.Add);
             int a = delObj(4, 2);
             int b = delObj1(4, 2);
             Console.WriteLine("the result of ss is {0} and the result of ss1 is {1}", a, b);
         }
     }
 class abc
     {
         public static int Sub(int i, int j)
         {
             return i * j;
         }

        public int Add(int x, int y)
         {
             return x + y;
         }
    }
 }

Multicasting with Delegates
1. We can call more than one function using the delegate.

2. For this we use the operators "+=" and "-=".

3. We use "+=" to add a method in the invocation list and the "-=" operator to remove a method    from the invocation list.

public delegate void my_multi_delegate(int x,int y);   
    //declaring a multi cast delegate with return type void

Multicasting Example:

mymultidelegate del = new mymultidelegate(abc.Add); //encapsulate the fun Add with the delegate

 del += new mymultidelegate (abc.Sub); //adding fun Sub in the invocation list with the help of += operator
  del += new mymultidelegate (abc.Mul); //adding fun Mul in the invocation list
 del(4, 2);
  //now we remove the function Add from the invocation lit with the help of -= operator
 del -= new mymultidelegate (abc.ss); // Now in the invocation list 2 method namely Sub and Mul remains
 
Multicast Delegate Support Void Return:

Because they are multicast, calling more than one method. Which return value would you use? How would you combine them (that is, how would the framework combine them)?
You can't use ref or out arguments either, for the same reason.

still its achiveable through GetInvocationList();

but manually call each handler from the invocation list of the delegate invocation list. Here is the example on how to do it:
 
var list = delegateInstance.GetInvocationList();
foreach (var handler in list) {
   DelegateName method = (DelegateName )handler;
   int retValue = method("some parameter");
   //do something with retValue...
}

Delegates system functions: Introduced in 3.5 Framework

Action is a delegate (pointer) to a method, that takes zero, one or more input parameters, but does not return anything.
list.Select(x => x.SomeProperty)

Func is a delegate (pointer) to a method, that takes zero, one or more input parameters, and returns a value (or refence).

Predicate is a special kind of Func often used for comparisons.

Though widely used with Linq, Action and Func are concepts logically independent of Linq.

No comments:

Post a Comment

Abstract Factory Vs Factory Method

Abstract Factory Provides an interface for creating families of related or dependent objects without specifying their concrete classes ...