Multicast Delegates Interview Questions

Collection of typically Interview Questions on Multicast delegates for interview preparation. you can also read more about delegatescallbacksMulticast DelegatesGeneric Delegates. Alternatively, you can also test your knowledge by an online free quiz for delegatesmulticast delegategeneric delegates.

Interview Question: What is Multicast delegate/ chaining delegates?

Answer: The capability of calling multiple methods on a single event is called as Multicast delegate or chaining delegates, Multicast delegates can be defined as delegates that have more than one element in their invocation list. In other words, it is a delegate that is subscribed by more than one method.


Interview Question: What will happen when any function throws exception from multicast delegate?

Answer: If the programme does not implement an exception, then the delegate will execute the program till exception and stop. Rest method from delegate won’t get executed.

If program handles the exception, then as per exception handling the program will work.


Interview Question: How will you handle Exceptions in multicast delegates?

Answer:

Suppose we have added multiple delegates to a single multicast delegate. Each of these individual delegates must be invoked, regardless of whether an unhandled exception is thrown within one of the delegates. But, once a delegate in a multicast delegate throws an unhandled exception, no more delegates are fired. we need a way to trap unhandled exceptions within each individual delegate while still allowing the rest of the delegates to fire.

To avoid breaking the chain, we have to gracefully handle exceptions in all functions. Use the GetInvocationList method. This method returns each individual delegate from a multicast delegate and, by doing so, allows us to invoke each delegate within the try block of an exception handler

Example

using System;

namespace Conversion
{
    class Program
    {
        public delegate void RectDelegate(double width, double height);

        static void Main()
        {
            var rect = new Rect();
            var rectDelegate = new RectDelegate(rect.GetArea);
            rectDelegate += rect.GetPerimeter;
            var delegates = rectDelegate.GetInvocationList();
            foreach (var deleg in delegates)
            {
                try
                {
                    deleg.DynamicInvoke(12.89, 12.11);
                }
                catch (Exception e)
                {
                    Console.WriteLine($"Exception in {deleg.Method.Name}");
                }
            }
            Console.ReadLine();
        }
    }
    public class Rect
    {
        public void GetArea(double width, double height)
        {
            Console.WriteLine(@"Area is {0}", (width * height));
        }
        public void GetPerimeter(double width, double height)
        {
            Console.WriteLine(@"Perimeter is in action");
        }
    }
}

Interview Question: What is InvocationList?

Answer: A Multicast Delegate has a linked list of delegates, called an invocation list. i.e. all delegate we assigned will be added in to list. So, When a multicast delegate is invoked, the delegates in the invocation list are called synchronously in the order in which they appear


Interview Question: What is output of below program and why?

using System;

namespace Operation
{
    public delegate void DemoDelegate(out int a);
    public class Program
    {
        static void Main()
        {
            var del = new DemoDelegate(Test1);
            del += Test2;
            int valueFromOutPutParameter;
            del(out valueFromOutPutParameter);
            Console.WriteLine($"result = {valueFromOutPutParameter}" );
            Console.ReadKey();
        }

        public static void Test1(out int ii)
        {
            ii = 10;
        }

        public static void Test2(out int ii)
        {
            ii = 20;
        }
    }
}

Answer: Result value is 20, In multicast delegate return last function value as result hence the output of above program is 20


Interview Question: Real time example of multicast delegate

Answer: we will take an example of an Order Processing System that makes use of the Multicast Delegate. Where We split the order processing into five small functions.

  • Get Cart Items
  • Calculate Order Price
  • Calculate Discount
  • Add Free Reward
  • Confirm the order

In which all above function requires customer id to perform some actions.

Online free Quiz to test Multicast Delegate knowledge and you can also read the tutorial about multicast delegate.

Leave a Reply

Your email address will not be published. Required fields are marked *