dynamic dispatch in c#

In this post i want to write about dynamic dispatch. So dynamic dispatch is used for code invocation and describes if a language uses compile time information or run time information to invoke a method. The two main concepts with dynamic dispatch are single dispatch and multible dispatch. I wanted to start with the simpler one and most common in object orientated languages, with single dispatch.

  • Single dispatch in c# is polymorphistic overriding of methods. The following example shows the c# code for overriding.
    public class Animal
    {       
        public virtual void VirtualShout()
        {
            Console.WriteLine("Animal virtual shouts");
        }
        public void Shout()
        {
            Console.WriteLine("Animal shouts");
        }
    }
    
    public class Cat : Animal
    {        
        public new void Shout()
        {
            Console.WriteLine("Cat shouts");
        }
    }
    
    public class SingleDispatch
    {
        public void Dispatch()
        {
            Animal animal = new Cat();
            animal.VirtualShout();
            animal.Shout();
        }
    }

    Single dispatch is normally implemented with virtual tables. That means the compiler produces a table with entries depending on the methods of the classes. In the Animal example the virtual table for the Animal and the Cat class looks the following way.

       
   Animal:
   vtableAnimal 

   Cat:
   vtableCat

Here we see the output of the single dispatch example:
SingleDispatchOutput
As we see if we call animal.VirtualShout() the decision which VirtualShout method is called (Animal or Cats one) is made at runtime. When the call gets executed the runtime looks at the virtual table and calls the method that stands in that virtual table. In our case the VirtualShout method of the Animal class is called because it is not ovverriden in the Cat class. In c# that mechanismus works only for virtual methodes (in java every method is automatic virtual). The Shout() method is not virtual and so the decision which method to call is choosen at compile time and does not change at runtime. The importand point why this virtual call mechanismus is called single dispatch is because the decision which method to call is made by the single object (If it is a Animal object it has a pointer to the Animal virtual table if it is a Cat it has a pointer to the Cat virtual table).

  • multible dispatch works with the dynamic keyword in c# the following example shows multible dispatch
    public class Owner
    {
        public virtual string Name
        {
            get { return "Owner"; }
        }
    }
    
    public class Mathias : Owner
    {
        public override string Name
        {
            get { return "Mathias"; }
        }
    }
    
    public class Animal
    {       
        public virtual void Shout(Owner owner)
        {
            Console.WriteLine("Animal Shouts to Owner object {0}", owner.Name);
        }
    
        public virtual void Shout(Mathias mathias)
        {
            Console.WriteLine("Animal Shouts to Mathias object {0}", mathias.Name);
        }
    }
    
    public class Cat : Animal
    {        
        public override void Shout(Owner owner)
        {
            Console.WriteLine("Cat Shouts to Owner object {0}", owner.Name);
        }
    
        public override void Shout(Mathias mathias)
        {
            Console.WriteLine("Cat Shouts to Mathias object {0}", mathias.Name);
        }
    }   
    
    public class MultibleDispatch
    {
        public void Dispatch()
        {
            Animal animal = new Animal();
            Owner owner = new Mathias();
            animal.Shout(owner);
            dynamic dynamicOwner = new Mathias();
            animal.Shout(dynamicOwner);
        }
    }

    Multible dispatch means that the decision which method should be called is made by the type of all the methodes parameter. In c# that decision is normally met at compile time, that means the compiler looks at the parameter types of the methodes and chooses the method that fits best. In the example we have two classes Owner and Mathias. Mathias is derifed from Owner. If we declare an Owner and make it an instance of Mathias, and call the Shout(owner) method the binding is made at compile time. That means the Shout method with the first method (Shout(Owner owner)) is called because the compiler uses the type of owner (for the compiler that type is Owner) to call the method. So that call is single dispatch because it uses only the instance objects type Animal to decide which method to call. If we want a call that recognises the correct type of the parameter (the type is Mathias not Owner) we have to move the decision which method to call to runtime. We do that with the dynamic keyword. If we build a dynamic object dynamicOwner that object is from type object and if the runtime makes the Shout(dynamicOwner) call it knows what type dynamicOwner really is (Mathias) and it chooses the correct method (Shout(Mathias mathias)). Here is the output of the Dispatch() method.
    MultibleDispatchOutput
    So now the decision is made by the parameter type and if we have more than one parameter, the decision is made by all the parameter types as we see in the following example.

    public class Animal
    {  
        public virtual void ShoutTwoOwners(Owner ownerOne, Owner ownerTwo)
        {
            Console.WriteLine(
                "Animal Shouts to Owner one object with name {0}, Owner two object with name {1}",
                ownerOne.Name, ownerTwo.Name);        
        }
    
        public virtual void ShoutTwoOwners(Owner ownerOne, Mathias mathiasTwo)
        {
            Console.WriteLine(
                "Animal Shouts to Owner one object with name {0} Mathias two object with name {1}",
                ownerOne.Name, mathiasTwo.Name);
        }
    
        public virtual void ShoutTwoOwners(Mathias mathiasOne, Owner ownerTwo)
        {
            Console.WriteLine(
                "Animal Shouts to Mathias one object with name {0} Owner two object with name {1}", 
                mathiasOne.Name, ownerTwo.Name);        
        }
    
        public virtual void ShoutTwoOwners(Mathias mathiasOne, Mathias mathiasTwo)
        {
            Console.WriteLine(
                "Animal Shouts to Mathias one object with name {0} Mathias two object with name {1}", 
                mathiasOne.Name, mathiasTwo.Name);        
        }
    }
    public class MultibleDispatch
    {
        public void Dispatch()
        {
            Animal animal = new Animal();        
            dynamic dynamicOwnerOne = new Mathias();
            dynamic dynamicOwnerTwo = new Owner();
            animal.ShoutTwoOwners(dynamicOwnerOne, dynamicOwnerTwo);
        }
    }

    Here is the output for that example that shows that the decision which method to call is made by multible Dispach (multible parameter are evaluated at runtime to make the correct method call)
    MultibleDispatchTwoParametersOutput

Advertisements

One Comment on “dynamic dispatch in c#”

  1. Corina says:

    I feel that is among the so much important information for
    me. And i’m glad studying your article. However should remark on some basic issues, The web site style is ideal, the articles is truly great : D. Just right activity, cheers


If you have a note or a question please write a comment.

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s