Quantcast
Channel: Odyssey to knowledge » C# Notes
Viewing all articles
Browse latest Browse all 4

Understanding Delegates

$
0
0
  1. Represents a class
  2. Type-Safe
  3. Delegate types are derived from delegate class (actually MultiCastDelegate subclass of delegate)
  4. delegate is sealed class
  5. Instantiated delegate is of type object so can be passed as parameter or assigned to property
  6. Can be used for both static as well as instance method
  7. Can be defined inside / ouside a class
  8. Can be used for anynchronous programing
  9. Can be used for event pub/sub
  10. Can combine multiple delegates into one to form multicast delegate
  11. A MulticastDelegate has a linked list of delegates, called an invocation list, consisting of one or more elements. When a multicast delegate is invoked, the delegates in the invocation list are called synchronously in the order in which they appear. If an error occurs during execution of the list then an exception is thrown.
  12. Can be called using the operator(). Invoke can’t be called directly

  13. public delegate void Del(string message);
    // Create a method for a delegate.
    public static void DelegateMethod(string message)
    {
        System.Console.WriteLine(message);
    }
    // Instantiate the delegate.
    Del handler = DelegateMethod;
    class MethodClass
    {
        public void Method1(string message) { }
        public void Method2(string message) { }
    }
    MethodClass obj = new MethodClass();
    Del d1 = obj.Method1;
    Del d2 = obj.Method2;
    Del d3 = DelegateMethod;

    // Call the delegate.
    handler(“Hello World”);

     //Both types of assignment are valid.
    Del allMethodsDelegate = d1 + d2;
    allMethodsDelegate += d3;
    //remove Method1
    allMethodsDelegate -= d1;

    // copy AllMethodsDelegate while removing d2
    Del oneMethodDelegate = allMethodsDelegate – d2;

  14. Delegates equality is established if both the delegate type aswell as function are equal
  15. public class zzz
    {
    	public static void Main()
    	{
    		aa a = new aa();
    		a.abc();
    		System.Console.ReadLine();
    	}
    }
    
    public class aa
    {
    	public delegate void xyz();
    	public delegate void pqr();
    	void  pqr1 ()
    	{
    	}
    
    	void  xyz1()
    	{
    	}
    
    	public void abc()
    	{
    		pqr d  = new pqr(pqr1);
    		pqr e  = new pqr(pqr1);
    
    		System.Console.WriteLine( d == e);
    		xyz f = new xyz(xyz1);
    
    		//Compile error
    		//System.Console.WriteLine( d == f);
    
    		xyz g = new xyz(pqr1);
    
    		//compile error
    		//System.Console.WriteLine( d == g);
    
    		System.Console.WriteLine( f == g);
    	}
    }

  16. Compiler Emitted Code
  17. class DelegateClass: System.MultiCastDelegate
                      {
                             // can't be called directly. Synchronous
                             public  Invoke(params_list);
                             // the last two are always the ending two params
                             public IAsyncResult BeginInvoke(param_list, AsyncCallBack callback, object asyncstate);
                             public EndInvoke(IAsyncResult ar);
                      }

  18. Async Delegate Execution Pattern
    1. Polling
    2. Waiting for Completion
    3. Completion Notification
    4. Fire and Forget
  19. You cannot execute a multicast delegate (one that calls more than one client) asynchronously. Each client must be called asynchronously in turn by processing the stored list of clients, the Invocation List. Failing to do this results in an ArgumentException being thrown.

Viewing all articles
Browse latest Browse all 4

Trending Articles