Tuesday, July 26, 2011

Delegate :C# ( Digging , Explain , Use it)

Delegate in C# (Digging and explain internals of Delegate)

Overview

.NET provides one nice characteristic that is delegate. Delegate is an object or instance which can hold reference of any function OR which can bind function. Referenced function may be static or any class object’s function. This can hold more than one function references. This also provides callback method. Developer’s perspective Delegate is two type single cast and multicast.
Every delegate base class is System.MulticastDelegate. System.MulticastDelegate class is inheriting from delegate class. Both classes are abstract class.
.NET provide standard constructor as follows:
Constructor for MultiCastDelegate : protected MulticastDelegate(object target, string method);
Constructor for Delegate : protected Delegate(object target, string method);

Declare Delegate

Declaring of the delegation is quite tricky, because when we declare delegation. We inform the compiler which kind of function signature’s reference could be hold by this delegate. Let’s explain by following: Fig-1.0  
Look at FIG-1.0:
 
  1. public is Access modifier .
  2. delegate is keyword which indicates this is delegate type.
  3. Return type of function signature.
  4. Delegate class name. (Explain below).
  5. Parameter of function signature.
  6. Parameter of function signature
3,5,6 are optional. Because these are depending on function signature, which function type we want bind or wrap with delegate.
Lets one more example:  protected delegate string DelegateReverse (string StrText) ;
It means this delegate class object can bind a function which function return type should be string with one string parameterDelegateReverse is delegate class name.
Question:   Look at Fig-1 DelegateFunction what is this?  Is this is an object or name of the delegate.
Answer:  This is sealed class. Look following example then we describe
//Code Sample - 1.0

using System;
using System.Collections.Generic;
using System.Text;
namespace DelegateCode
{
   class DelExample
    {
       // Delegate declaration.  
      public delegate string StringOperation(string myString);
        static void Main(string[] args)
        {
            // Create instance of class for accessing its function
           DelExample objEx = new DelExample();
                    // create instance of Delegate Class 
           StringOperation Objdelegate = new      StringOperation(objEx.MyOperationReverseString);
                 // Call delegate object and store result in appropriate variable
           string result = Objdelegate("ABC DEF GHI");           
          // Get Type
            Type Tp = Objdelegate.GetType();
        // Output 
         Console.WriteLine("Is this Class : " + Tp.IsClass.ToString()+ "\n");
         Console.WriteLine("Inherit from : " + Tp.BaseType.ToString() + "\n");
         Console.WriteLine("Is Abstract : " + Tp.IsAbstract.ToString() + "\n");
         Console.WriteLine("Is ByRef : " + Tp.IsByRef.ToString() + "\n");
         Console.WriteLine("Is Sealed : " + Tp.IsSealed.ToString() + "\n");
         Console.WriteLine("Output of Delegate : " + result+"\n");
         Console.WriteLine("Output by Invoke : " + Objdelegate.Invoke("ABC DEF GHI"));
         Console.Read();
        }
        // definition of the function MyOperationReverseString
        public string MyOperationReverseString(string test)
        {
            char[] Spl = test.ToCharArray();
            test = string.Empty;
            for (int icnt = 0; icnt < Spl.Length; icnt++)
            {
                test = Spl[icnt] + test;
            }
            return test;
        }
    }
}
 
Output of this program Fig – 2.0
Consider Fig – 2.0 output.  In the above code the following line
public delegate string StringOperation(string myString);
StringOperation is sealed Class. This inherited by System.MultiCastDelegate and not is abstract.
Look last two lines of output. We can call delegate by invoke method or by passing parameter without invoke method. 

How C# Compiler Understands

Whenever compiler encountered delegate declaration like:
public delegate string StringOperation(string myString);
Then compiler generate following code:
public sealed class StringOperation: System.MulticastDelegate
{
  public StringOperation (object target, int method);
  public virtual void Invoke(string myString);
  public virtual IAsyncResult BeginInvoke(string myString,
  AsyncCallback callback, object obj);
  public virtual void EndInvoke(IAsyncResult result);
}
 
Look Program-1 IL-DASM view (Fig-3.0)
You can see .ctor: void (Object ,native int) ( This is similar constructor asSystem.MulticastDelege constructor.)
Invoke method should have same signature of the referenced function 

History Delegate Cannot Declare and Use as Field

//Code Sample – 2.0:
namespace DelegateExample
{
   public delegate string MyDelegate(string myString);
    interface MyCallable
    {
        int GetInfo();
delegate int CalcArea(int x, int y); // Error : interfaces cannot declare types      
MyDelegate ObjDelegae; // Error : Interfaces cannot contain fields    
    }
  }

//Code Sample – 3.0:
public void DispachCall(int tStr)
        {
           public delegate string MyDelegate(string myString); // Error          
            
           
        }

History Difference b/w Single and Multicast Delegate

Developer’s perspective Delegate is two type single cast and multicast. Although ".NET" has provides two classes for the delegate. 
Singlecast delegate vs Multicast delegate have not any declaration and implementation differences. This is only perspective.  
Even I read more articles related delegate, they told Multicast delegate cannot wrap / bind a function which returns value. But I never seem like this.
  1. Delegate class ( Abstract class).
  2. MulticastDelegate (Abstract class inherited by Delegate class).
Every delegate in the .NET is Multicast delegate. Then…
Question : So what is Singlecast Delegate ?
Answer: If delegate object have only one function’s reference then its call single cast delegate. If delegate object references more than one functions this called MulticastDelegate
When delegate object refer or bind multiple functions references then all functions reference store as linked list.  All functions will call in sequence as they assigned.
Example : let’s we have delegate object  ObjDel.
ObjDel bind or wrap three functions in sequence respectively Add , Mul , Divide. All three functions return integer type.
Let’s calling delegate:
Int result = ObjDel.invoke(15,5); It will return 3.
If function wrap in sequence Add, Divide, Mul.
Result = ObjDel.invoke(15,5). It will return 75.
It always returns the value of last function. So what about the other function return values ?

Keypoint: 

If any delegate object bind or wrap 5 return type functions. Then only 5th functions will return value but rest 4 functions could not return value. So always end of calling function return value. Rest 4 functions return value will ignored.

Delegate Asynchronously

Note : Simple overview for delegate as asynchronous call. More explain in next article 
Recall Fig-3.0 there is IL-DASM view. StringOperation delegate have Invoke and BeginInvoke function. These two functions created by compiler at run time.
In C# we can achieve asynchronous function call using delegate.
By default every function in the C# is the synchronous. But we can call synchronous function as asynchronous.
The Invoke method calls the target method on the current thread. But BeginInvoke method is called with different thread, the common language runtime (CLR) queues the request and returns immediately to the caller.
BeginInvoke can call by only single cast delegate otherwise its get run time exception (ArgumentException) is “The delegate must have only one target”.

About BeginInvoke

.NET implements BeginInvoke function for each Delegate class. So whenever compiler encountered delegate declaration its implement three method for each delegate class. 
InvokeBeginInvoke and EndInvoke. Each BeginInvoke have corresponding EndInvoke.

Key Point

Always call EndInvoke to complete your asynchronous call does not matter technique of the calling.
BeginInvoke have three parameters. BeginInvoke return IAsyncresult
S.No

Description
1.parameters of the function which calling

2.AsynchCallbackdelegate void AsyncCallback( IAsyncResult ar );
3.
System.Object

AsyncCallback (IAsyncResult  Ar).

Key Point

IAsyncresult is using for monitoring and state or progress of the asynchronous function call. This is return by BeginInvoke and it is also parameter for AsyncCallback function. Its declaration is.
public interface IAsyncResult 
{ 
    object AsyncState{ get; } 
    WaitHandle AsyncWaitHandle { get; } 
     bool CompletedSynchronously { get; } 
    bool IsCompleted { get; } 
}

Key Point

Call delegate using Invoke method. Then it’s run at same thread.
Look at following simple code. I called same delegate using Invoke and BeginInvoke method. And write thread ID for both call.

Code Sample – 4.0

using System;
using System.Threading;

namespace TestMultipuleDelegate
{
    class MultiCastTest : TestMultipuleDelegate.IMultiCastTest
    {

       delegate int Calc(int x, int y,out int id);
       
        static void Main(string[] args)
        {

            int thid = 0;
            MultiCastTest ClObj = new MultiCastTest();

            Calc objCalc = new Calc(ClObj.Add);    
           
                      
            // Get current thread ID
            thid = AppDomain.GetCurrentThreadId();

            Console.WriteLine("     ");
            Console.WriteLine(" Main Application Thread ID : " + thid.ToString()); Console.WriteLine();
            
            // Call delegate by Invoke method (synchronous call 
            int Result = objCalc.Invoke(8, 8, out thid);

            Console.WriteLine("Invoke method completed "); Console.WriteLine();
            
            // Call delegate by BeginInvoke : Asynchronous call 
            IAsyncResult rs = objCalc.BeginInvoke(234, 6, out thid, null,null);
           
            Console.WriteLine("Begin Invoke has been start."); Console.WriteLine();
          //  Calling continue Mul funtion along Main thread.
            Console.WriteLine( "Resule Mul " + ClObj.Mul(10, 8).ToString());
            // Waiting till completed the Asynchronous call completed.
            while (rs.IsCompleted == false)
            {

                Console.WriteLine("On processing..."); Console.WriteLine();
                Thread.Sleep(1000);
            }
           //// Get the result of Asynchronous ,calling functions
           Result = objCalc.EndInvoke(out thid,rs);
            Console.WriteLine("Result : " + Result.ToString()); Console.WriteLine();
            Console.WriteLine("Process completed"); Console.WriteLine();
            Console.ReadKey();
        }
       
        public int Add(int x, int y,out int thid)
        {
            
            Console.WriteLine("In side Calling function Add  : "); Console.WriteLine();
            // Get calling function thread ID
            thid = AppDomain.GetCurrentThreadId();
            Console.WriteLine(" Calling function Thread ID : " + thid.ToString());
            Console.WriteLine();
            Thread.Sleep(1000);
            return x + y;
        }

        public int Mul(int a, int b)
        {
Console.WriteLine("Inside Multipule function thred ID. "+     AppDomain.GetCurrentThreadId().ToString()); Console.WriteLine();
            return a*b;
        }
                        
    }
}

// Another .CS file
using System;
namespace TestMultipuleDelegate
{
    interface IMultiCastTest
    {
        int Add(int x, int y, out int thid);
        int Mul(int a, int b);
    }
}
 
Fig – 4.0 ( Code Sample – 4.0 outputs)
We can see at Fig-4.0: (Code Sample – 4.0 outputs)
Main thread id = Invoke method function call thread id = simple function call immediate after BeginInvoke function call. But BeginInvoke function call thread id is 2992 different.

Some fact about the Delegate

  • Delegate is object and by default private type access modifier.
  • Delegate class generated by C# compiler according its declaration.
  • Delegate class object can hold reference of static or any class object’s function.
  • Delegate class is sealed class which generated by the C# compiler during run time.
  • Delegate is keyword so any function cannot return delegate type.
  • Delegate cannot use as field of the class.
  • Delegate provides asynchronous and synchronous call.

No comments:

Post a Comment