static void Main(string[] args) { //Approach 1 SampleDelegate del1, del2, del3, del4; del1 = new SampleDelegate(SampleMethod1); del2 = new SampleDelegate(SampleMethod2); del3 = new SampleDelegate(SampleMethod3); del4 = del1 + del2 + del3 - del2; del4(); // Multicast delegate //Approach 2 SampleDelegate del = new SampleDelegate(SampleMethod1); //Register del += SampleMethod2; // Register del += SampleMethod3; // Register del -= SampleMethod2; // De register del(); // Multicast delegate SampleDelegate2 delInt = new SampleDelegate2(SampleMethod4); delInt += SampleMethod5; Console.WriteLine(delInt()); // Last invoked method's value will be shown Console.Read(); }
static void Main(string[] args) { SampleDelegate del1, del2, del3, del4; SampleDelegate del5 = new SampleDelegate(SampleMethodOne); del5 += SampleMethodTwo; del1 = new SampleDelegate(SampleMethodOne); del2 = new SampleDelegate(SampleMethodTwo); del3 = new SampleDelegate(SampleMethodThree); del4 = del1 + del2 + del3; del4(); del4 -= del2; Console.WriteLine(Environment.NewLine); del4(); Console.WriteLine(Environment.NewLine); del5(); Console.WriteLine(Environment.NewLine); SampleDelegate2 del6 = new SampleDelegate2(SampleMethodFour); del6 += SampleMethodFive; Console.WriteLine(del6()); int num = -1; SampleMethodSix(out num); Console.WriteLine(num); }
static void Main() { // 匿名メソッドによるデリゲート SampleDelegate2 d = delegate(int n) { return(-n); }; // ;が必要 Console.WriteLine(d(3)); // 結果:-3 }
static void Main(string[] args) { // Approach 1: SampleDelegate1 del1 = new SampleDelegate1(SampleMethodOne); SampleDelegate1 del2 = new SampleDelegate1(SampleMethodTwo); SampleDelegate1 del3 = new SampleDelegate1(SampleMethodThree); // In this example del4 is a multicast delegate. You use +(plus) // operator to chain delegates together and -(minus) operator to remove. SampleDelegate1 del4 = del1 + del2 + del3 - del2; del4(); // Approach 2: // In this example del is a multicast delegate. You use += operator // to chain delegates together and -= operator to remove. SampleDelegate2 delAp2 = new SampleDelegate2(SampleMethodOne); delAp2 += SampleMethodTwo; delAp2 += SampleMethodThree; delAp2 -= SampleMethodTwo; del2(); // Multicast delegate with an int return type: SampleDelegate3 delAp3 = new SampleDelegate3(SampleMethodOneInt); delAp3 += SampleMethodTwoInt; // The ValueReturnedByDelegate will be 2, returned by the SampleMethodTwo(), // as it is the last method in the invocation list. int ValueReturnedByDelegate = delAp3(); Console.WriteLine("Returned Value = {0}", ValueReturnedByDelegate); // Multicast delegate with an integer output parameter: SampleDelegate4 delAp4 = new SampleDelegate4(SampleMethodOne); delAp4 += SampleMethodTwo; // The ValueFromOutPutParameter will be 2, initialized by SampleMethodTwo(), // as it is the last method in the invocation list. int ValueFromOutPutParameter = -1; delAp4(out ValueFromOutPutParameter); Console.WriteLine("Returned Value = {0}", ValueFromOutPutParameter); }
public static void Main() { /* * SampleDelegate del = new SampleDelegate(SampleMethodOne); * del(); */ /* * SampleDelegate del1, del2, del3, del4; * * del1 = new SampleDelegate(SampleMethodOne); * del2 = new SampleDelegate(SampleMethodTwo); * del3 = new SampleDelegate(SampleMethodThree); * * del4 = del1 + del2 + del3 - del2; * del4(); */ SampleDelegate del = new SampleDelegate(SampleMethodOne); del += SampleMethodTwo; //del += SampleMethodThree; //del -= SampleMethodOne; int DelegateReturnedValue = del(); Console.WriteLine("DelegateReturnedValue = {0}", DelegateReturnedValue); SampleDelegate2 dell = new SampleDelegate2(SampleMethodOne); dell += SampleMethodTwo; int DelegateOutputParameterValue = -1; dell(out DelegateOutputParameterValue); Console.WriteLine("DelegateOutputParameterValue = {0}", DelegateOutputParameterValue); }