static void Main(string[] args) { MyVoidDelegate myVoidDelegate = new MyVoidDelegate(MyVoidMethod);//必須符合簽章 回傳型別void 與1個string參數 myVoidDelegate += MyVoidMethod2; myVoidDelegate -= MyVoidMethod2; myVoidDelegate.Invoke("Mickey"); }
static void Main(string[] args) { MyVoidDelegate myDeleg = new MyVoidDelegate(PrintInteger); myDeleg += SomeMethod; //MyVoidDelegate myDeleg2 = new MyVoidDelegate(SomeMethod); myDeleg(100); myDeleg?.Invoke(100); myDeleg -= SomeMethod; MyStringDelegateByIntegers del3 = new MyStringDelegateByIntegers(SomeOtherMethod); PassSomeDelegate(myDeleg); Console.WriteLine(del3(5, 6)); var person = new Person(); myDeleg += person.SomePersonMethod; Console.WriteLine(myDeleg.Target?.GetType().Name); }
public static void PassSomeDelegate(MyVoidDelegate del) { del?.Invoke(5); }