internal void DoCaptureThing() { SomeAction action = MakeDelegate(); action(); action(); }
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>"); }
internal void SomeActionList() { SomeAction[] someActions = new SomeAction[10]; int x; for (int i = 0; i < 10; ++i) { x = 0; int y = 0; someActions[i] = delegate { Console.WriteLine("{0}, {1}", x, y); x++; y++; }; } someActions[0](); // Prints 0, 0 someActions[0](); // Prints 1, 1 someActions[0](); // Prints 2, 2 someActions[1](); // Prints 3, 0 someActions[0](); // Prints 4, 3 x = 10; someActions[2](); // Prints 10, 0 }
static void Main(string[] args) { SomeAction action1 = new SomeAction(ShowText); action1 += ShowMessage; SomeAction action2 = ShowText; action1.Invoke(""); action2(" => Action2"); Console.ReadLine(); }
static void Main(string[] args) { var random = new Random(123456); var i = 10; IAction act = new SomeAction(); while (i-- > 0) { var n = random.Next(); dynamic curStep = (n % 2) == 0 ? (dynamic) new State1() : new State2(); UpdateState(act, curStep); } Console.ReadKey(); }
static void Main(string[] args) { SomeAction action1 = new SomeAction(Showtext); action1.Invoke("好的"); action1("原來如此"); SomeAction action2 = ShowMessage; action2("第二個"); action1("原來如此"); Console.ReadLine(); }
static void Main(string[] args) { // 標準寫法 SomeAction action1 = new SomeAction(ShowText); action1 += ShowMessage; // 也可以這樣寫 SomeAction action2 = ShowText; // 標準寫法 action1.Invoke("第一個"); action2 += ShowMessage; //也可以這樣寫 action2("第二個"); Console.ReadLine(); }
private static void UpdateState(SomeAction act, State2 s1) { Console.WriteLine("State2 SomeAction Updated."); }
// CONSTRUCTORS public Timer(int seconds, SomeAction action) { IntervalSeconds = seconds; this.action = action; }
// CONSTRUCTORS public Timer(int seconds, SomeAction action) { IntervalSeconds = seconds; this.action = action; }
public TestObj(string name, SomeAction action, string value) { Name = name; Action = action; Value = value; }