Multicast delegates in c#

In yesterday’s post We learn about Delegates and how we can use delegates in C#. In today’s blog post we are going to learn about Multicast delegates.

What is Multicast Delegates?

As we all know we can assign methods as object to delegate and later on we can call that method with the help delegates. We can also assign more then methods to delegates that is called Multicast delegates. It’s provide functionality to execute more then method at a time. It’s maintain delegates as invocation list (linked list).

Let’s understands that via a example. We are going to use yesterday’s example and then we will extend that code multicast delegates. Following code I have written to demonstrate the multicast delegates.

using System;
namespace Delegates
{
    class Program
    {
        public delegate void CalculateNumber(int a, int b);
        static void Main(string[] args)
        {
            int a = 5;
            int b = 5;
            CalculateNumber addNumber = new CalculateNumber(AddNumber);
            CalculateNumber multiplyNumber = new CalculateNumber(MultiplyNumber);
            CalculateNumber multiCast = (CalculateNumber)Delegate.Combine (addNumber, multiplyNumber);
            multiCast.Invoke(a,b); 
            Console.ReadLine(); 
        }
        public static void AddNumber(int a, int b)
        {
            Console.WriteLine("Adding Number");
            Console.WriteLine(5 + 6); 
        }
        public static void  MultiplyNumber(int a, int b)
        {
            Console.WriteLine("Multiply Number");
            Console.WriteLine(5 * 6); 
        }
    }
}

As you can see in the above code I have created two method one for adding two numbers and another for multiply two number. After that I have created two same CalculateNumber delegates addNumber and multiplyNumber then I have create a multicast delegates multiCast with combining two delegates. Now I want to call this both method so I have used Invoke method to call this delegates.

As now our code is let’s run the application. Following is a output as expected.

image

As you can we can execute multiple methods with multicast delegates the only thing you need to take care is that we need to type for both delegates. That’s it. Hope you like it. Stay tuned for more.. Till then happy programming.

Shout it
Published Friday, April 13, 2012 1:29 AM by Jalpesh P. Vadgama

Comments

# re: Multicast delegates in c#

Thursday, April 12, 2012 8:57 PM by annoying guy

good example, but your a & b values aren't being used and your multiply method isn't multiplying...

# re: Multicast delegates in c#

Friday, April 13, 2012 3:16 PM by term papers for sale

Great so nice to coment here!

# re: Multicast delegates in c#

Friday, April 13, 2012 4:12 PM by Jalpesh P. Vadgama

@Annoying guy- I have corrected that.

# re: Multicast delegates in c#

Saturday, April 14, 2012 5:37 PM by Kishor

Example gives an error at "Combine".

# re: Multicast delegates in c#

Sunday, April 15, 2012 2:12 AM by Jalpesh P. Vadgama

@kishor- What errors are you getting there?

# re: Multicast delegates in c#

Sunday, July 8, 2012 5:21 AM by term paper writer

great) liked everything very much) keep it up and dont stop)

# re: Multicast delegates in c#

Wednesday, July 11, 2012 9:59 AM by http://www.idealscorp.com/

very good post! need to say you did a great job and i really appreciate it!

# re: Multicast delegates in c#

Wednesday, July 18, 2012 9:30 AM by Jalpesh P. Vadgama

@Indealcorp - thanks

# re: Multicast delegates in c#

Wednesday, July 25, 2012 2:27 AM by essaywriter.org

Written simply and tastefully. It’s pleasant to read. Thank u.

# re: Multicast delegates in c#

Thursday, August 30, 2012 3:02 AM by Santosh Ainapur

Very good example on multicast delegate.

# re: Multicast delegates in c#

Monday, November 26, 2012 4:33 AM by A-Writer.com

I’m so glad and honoured to be featured here! You don’t even know how much I

# re: Multicast delegates in c#

Friday, November 30, 2012 1:07 AM by research paper samples

Now I want to call this both method so I have used Invoke method to call this delegates.

# re: Multicast delegates in c#

Friday, January 4, 2013 3:08 AM by WisenTechnologies

Delegates supports both synchronous and  asynchronous method calling