Skip to main content

Multicast Delegates :: Advance : C# programming for c++ programmers



Defination:

A Multicast delegate is a delegate that has references to more than one function.

When you call a multicast delegate, all the functions the delegate is pointing to, are invoked.



Example 1:

using System;
namespace demo
{
    public delegate void demoDelegate();
    
    public class demoSample
    {
        static void Main()
        {
            demoDelegate del1 = new demoDelegate(demoMethodOne);
            demoDelegate del2 = new demoDelegate(demoMethodTwo);
            demoDelegate del3 = new demoDelegate(demoMethodThree);
            


            demoDelegate del4 = del1 + del2 + del3 - del3;
            

            del4();  // del4 invokes de1 and del2, since -del3 is invoked, call to del3 is removed.
        }


        public static void demoMethodOne()
        {
            Console.WriteLine("SampleMethodOne Invoked");
        }


        public static void demoMethodTwo()
        {
            Console.WriteLine("SampleMethodTwo Invoked");
        }


        public static void demoMethodThree()
        {
            Console.WriteLine("SampleMethodThree Invoked");
        }
    }
}


Note: 

  • Delegate objects can be composed using the "+" operator.
  • The "-" operator can be used to remove a component delegate from a composed delegate.
  • If the delegate has a return type other than void and if the delegate is a multicast delegate, only the value of the last invoked method will be returned.
  • If the delegate has an out parameter, the value of the output parameter, will be the value assigned by the last method.





Example 2: Instead of creating multiple delegate object, we can directly assign method through += and -=.


namespace demo
{
    public delegate void demoDelegate();
    
    public class demoSample
    {
        static void Main()
        {
            demoDelegate del = new demoDelegate(demoMethodOne);
   del += demoMethodTwo;
   del += demoMethodThree;
            del -= demoMethodTwo;
        }


        public static void demoMethodOne()
        {
            Console.WriteLine("SampleMethodOne Invoked");
        }


        public static void demoMethodTwo()
        {
            Console.WriteLine("SampleMethodTwo Invoked");
        }


        public static void demoMethodThree()
        {
            Console.WriteLine("SampleMethodThree Invoked");
        }
    }
}

Comments

Popular posts from this blog

const used with functions

const used with functions : class Dog {    int age;    string name; public:    Dog() { age = 3; name = "dummy"; }         // const parameters and these are overloaded functions    void setAge(const int& a) { age = a; }    void setAge(int& a) { age = a; }         // Const return value    const string& getName() {return name;}         // const function and these are overloaded functions    void printDogName() const { cout << name << "const" << endl; }  // value of name can't be modified    void printDogName() { cout << getName() << " non-const" << endl; } }; int main() {    Dog d;    d.printDogName();        const Dog d2;    d2.printDogName();     }

Structured Bindings

Returning multiple Values from function C++ 11 vs C++ 17 Returning compound objects Iterating over a compound collection Direct initialization Returning multiple Values from function C++ 11 vs C++ 17 :  C++ 11 (std::tie): std::tuple mytuple() {     char a = 'a';     int i = 123;     bool b = true;     return std::make_tuple(a, i, b);  // packing variable into tuple } To access return value using C++ 11, we would need something like: char a; int i; bool b; std::tie(a, i, b) = mytuple();  // unpacking tuple into variables Where the variables have to be defined before use and the types known in advance. C++ 17 : auto [a, i, b] = mytuple(); Returning compound objects :  This is the easy way to assign the individual parts of a compound type (such as a struct, pair etc) to different variables all in one go – and have the correct types automatically assigned. So let’s have a look at an ...

POST 1 : std::thread in c++ | C++ 11

std::thread   C++  Thread support library std::thread. Threads enable programs to execute across several processor cores. Defined in header Class : thread Member types : native_handle_type Data Member  id    //represents the id of a thread (public member class) Member functions (constructor)    //constructs new thread object (public member function) (destructor)    //destructs the thread object, underlying thread must be joined or detached  (public member function) operator=    //moves the thread object  Observers  // Covered in NEXT POST Operations    // Covered in NEXT POST Constructor :   #include   void f1 ( int n ) { for ( int i = 0 ; i < 5 ; ++ i ) { std:: cout << "Thread 1 executing \n " ; ++ n ; std:: this_thread :: sleep_for ( std:: chrono :: milliseconds ( 10 ) ) ; ...