public static void Main(string[] args)
    {
        C1 c1 = new C1();      // create instance of class that raises event
        L1 l1 = new L1();      // create instance of 1st class that handles event
        L2 l2 = new L2();      // create instande of 2nd class that handles event

        // no handlers are yet registered on the event
        try
        {
            c1.Raise();     // will crash and output exception as no handler set yet
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }

        c1.SafeRaise();     // safe, will print message

        l1.AddListener(c1); // add as listener
        c1.Raise();         // prints handled message

        l2.AddListener(c1); // add 2nd listener
        c1.Raise();         // both print handled message

        Console.ReadLine(); // stop console from closing
    }
示例#2
0
    public static void Main(string[] args)
    {
        C1 c1 = new C1();
        L1 l1 = new L1();
        L2 l2 = new L2();

        try
        {
            c1.Raise();     // will crash
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }

        c1.Raise2();     // safe

        l1.AddListener(c1);
        c1.Raise();
        l2.AddListener(c1);     // more then 1 listener possible
        c1.Raise();

        Console.ReadLine();
    }