protected void Page_Load(object sender, EventArgs e)
    {
        SomeAction action = ShowMessage1;

        action += ShowMessage2;
        action += ShowMessage3;
        action += ShowMessage4;
        action += ShowMessage5;
        action.Invoke("-");

        Response.Write("************R1*************<br>");

        //實體化一個委派物件,並將符合委派方法簽章的ShowMessage1方法放到委派物件中
        SomeAction action2 = new SomeAction(ShowMessage1);

        action2 += ShowMessage2;
        action2 += ShowMessage3;
        action2 += ShowMessage4;
        action2 += ShowMessage5;
        action2("-");

        Response.Write("************R2*************<br>");
        //不須宣告委派型別,Action就是一個委派型別
        Action <string> action3 = ShowMessage5;

        action3 += ShowMessage4;
        action3 += ShowMessage3;
        action3 += ShowMessage2;
        action3 += ShowMessage1;
        action3("-");

        Response.Write("************R3*************<br>");
    }
Beispiel #2
0
        static void Main(string[] args)
        {
            SomeAction action1 = new SomeAction(ShowText);

            action1 += ShowMessage;
            SomeAction action2 = ShowText;

            action1.Invoke("");
            action2(" => Action2");
            Console.ReadLine();
        }
Beispiel #3
0
        static void Main(string[] args)
        {
            SomeAction action1 = new SomeAction(Showtext);

            action1.Invoke("好的");
            action1("原來如此");

            SomeAction action2 = ShowMessage;

            action2("第二個");

            action1("原來如此");

            Console.ReadLine();
        }
Beispiel #4
0
        static void Main(string[] args)
        {
            // 標準寫法
            SomeAction action1 = new SomeAction(ShowText);

            action1 += ShowMessage;

            // 也可以這樣寫
            SomeAction action2 = ShowText;

            // 標準寫法
            action1.Invoke("第一個");
            action2 += ShowMessage;

            //也可以這樣寫
            action2("第二個");

            Console.ReadLine();
        }