public void Main() { Derivded x = new Derivded(); SampleDelegate factory = new SampleDelegate(x.CandidateAction); factory("test"); }
static void Main(string[] args) { SampleDelegate SD1, SD2, SD3,SD4; SD1 = new SampleDelegate(SampleMethod1); SD2 = new SampleDelegate(SampleMethod2); SD3 = SD1 + SD2; SD3(); //This is a multicast delegate SD4 = SD1 + SD2 - SD1;//like the + sign , - sign can also be used to remove the delegates (here , SD4 = SD2 basically) SD4(); SampleDelegate SD5 = new SampleDelegate(SampleMethod1); SD5 += SampleMethod2; //Another way to multicast delegates SD5();//Here , we don't need to create many instances of the delegate to make it multicast Console.ReadKey(); ReturningValueDelegate RVD;//Demonstrating return value RVD = new ReturningValueDelegate(ReturningValueMethod1); RVD += ReturningValueMethod2; int ReturnedValue = RVD(); Console.WriteLine("Returned Value is {0}", ReturnedValue); Console.ReadKey(); OutputParameterDelegate OPD; //Demonstrating output parameter OPD = new OutputParameterDelegate(OutputParameterMethod1); OPD += OutputParameterMethod2; int OutParameter = -1; OPD(out OutParameter); Console.WriteLine("Final output parameter is {0}", OutParameter); Console.ReadKey(); }
static void Main(string[] args) { //Rectange(); program p = new program(); #region MathDelegate //MathDelegate del1 = new MathDelegate(Add); //MathDelegate del2 = new MathDelegate(program.Sub); //MathDelegate del3 = p.Mul; //MathDelegate del4 = new MathDelegate(p.Div); //MathDelegate del5 = del1 + del2 + del3 + del4; //del5.Invoke(20, 5); //Console.WriteLine(); //del5 -= del3; //del5(22, 7); #endregion SampleDelegate sampleDelegate = new SampleDelegate(MethodOne); sampleDelegate += MethodTwo; int ValueReturnedByDelegate = sampleDelegate.Invoke(2); Console.WriteLine($"Returned Value is {ValueReturnedByDelegate}"); Console.ReadKey(); }
static void Main(string[] args) { SampleDelegate samp4, samp1, samp2, samp3; SampleDelegate del1 = new SampleDelegate(SampleMethodOne); del1 += SampleMethodTwo; del1(); samp1 = new SampleDelegate(SampleMethodOne); samp2 = new SampleDelegate(SampleMethodTwo); samp3 = new SampleDelegate(SampleMethodThree); samp4 = samp1 + samp2 + samp3; samp4(); Console.WriteLine("------------"); SampleDelegate1 Sdel1 = new SampleDelegate1(SampleMOne); Sdel1 += SampleMTwo; int DelegateRetValue; Sdel1(out DelegateRetValue); Console.WriteLine("Value of Delegate {0}", DelegateRetValue); Console.ReadLine(); }
public void NonGeneric() { // Assigning a method with a matching signature // to a non-generic delegate. No conversion is necessary. SampleDelegate dNonGeneric = ASecondRFirst; // Assigning a method with a more derived return type // and less derived argument type to a non-generic delegate. // The implicit conversion is used. SampleDelegate dNonGenericConversion = AFirstRSecond; // Assigning a method with a matching signature to a generic delegate. // No conversion is necessary. SampleGenericDelegate <Second, First> dGeneric = ASecondRFirst; // Assigning a method with a more derived return type // and less derived argument type to a generic delegate. // The implicit conversion is used. SampleGenericDelegate <Second, First> dGenericConversion = AFirstRSecond; SampleGenericDelegate <Second, Second> dGenericConversion2 = AFirstRSecond; // in A, out T // 1 //dGenericConversion = dGenericConversion2; // 2 //var genList = new List<SampleGenericDelegate<Second, First>>(); //genList.Add(dGenericConversion); //genList.Add(dGenericConversion2); }
static void main() { //Normal invoking of a delegate //SampleDelegate del = new SampleDelegate(SampleMethodOne); //del(); //1. The first method to mulitcast a Delegate SampleDelegate del1, del2, del3, del4; del1 = new SampleDelegate(SampleMethodOne); del2 = new SampleDelegate(SampleMethodTwo); del3 = new SampleDelegate(SampleMethodThree); //we can chain the delegates together using a + operator del4 = del1 + del2 + del3; del4(); //this delegate del4 is a mulitcast delegate and it will invoke all the 3 methods //if we cant to exclude a delegate use a minus sign del4 = del1 + del2 + del3 - del2; del4(); //2. Second method to invoke a multicast delegate SampleDelegate del = new SampleDelegate(SampleMethodOne); del += SampleMethodTwo; //we use + / += to register a method with the delegate del += SampleMethodThree; del -= SampleMethodOne; //we use -/-= to unregister a method from a delegate del(); //the methods in the invokation list are invoked in the same order they are registered }
public static void Main(string[] args) { // Here is the Code that uses the delegate defined above. SampleDelegate sd = new SampleDelegate(CallMeUsingDelegate); sd.Invoke("FromMain"); }
static void Main(string[] args) { DelegateClass obj = new DelegateClass(); SampleDelegate sd = new SampleDelegate(obj.DelegateMethod); sd(); // invoke obj.DelegateMethod() indirectly. }
static void Main(string[] args) { //SampleDelegate sampleDelegate = new SampleDelegate(SampleMethod1); //sampleDelegate(); SampleDelegate del1, del2, del4; del1 = new SampleDelegate(SampleMethod1); del2 = new SampleDelegate(SampleMethod2); // del3 = new SampleDelegate(SampleMethod3);4 del4 = del1 + del2 - del2; //delegate del4 point all of the method and its called multicast delegat // we can remove by using -del2 // we use + to add //del4(); //other way of using or making multicast delegatep SampleDelegate del = new SampleDelegate(SampleMethod1); del += SampleMethod2; // del += SampleMethod3; //del -= SampleMethod2; //int delegatereturnValue = del();// this to invoke delegate int delegateOutPutParameterValue = 5; del(out delegateOutPutParameterValue); Console.WriteLine(delegateOutPutParameterValue); }
static void Main(string[] args) { SampleDelegate samp4, samp1, samp2, samp3; SampleDelegate del1 = new SampleDelegate(SampleMethodOne); del1 += SampleMethodTwo; del1(); samp1 = new SampleDelegate(SampleMethodOne); samp2 = new SampleDelegate(SampleMethodTwo); samp3 = new SampleDelegate(SampleMethodThree); samp4 = samp1 + samp2 + samp3 ; samp4(); Console.WriteLine("------------"); SampleDelegate1 Sdel1 = new SampleDelegate1(SampleMOne); Sdel1 += SampleMTwo; int DelegateRetValue ; Sdel1(out DelegateRetValue); Console.WriteLine("Value of Delegate {0}", DelegateRetValue); Console.ReadLine(); }
public STR_TEST(int _Weight, SampleDelegate _sample_delegate) { Weight = _Weight; sample_delegate = _sample_delegate; param = new PARAM(100); }
static void Main() { //IComparer<Circle> areaComparer = new CircleComparer(); IComparer <IShape> areaComparer = new AreaComparer(); ComparisonHelper <IShape, Circle> areaComp = new ComparisonHelper <IShape, Circle>(areaComparer); List <Circle> circles = new List <Circle>(); circles.Add(new Circle(2)); circles.Add(new Circle(20)); circles.Add(new Circle(12)); circles.Add(new Circle(14)); circles.Sort(areaComp); foreach (Circle c in circles) { Console.WriteLine($"Circle radius:{c.Radius}"); } //委托的变体 SampleDelegate dNonGeneric = ASecondRFirst; SampleDelegate dNonGenericConversion = AFirstRSecond; SampleGenericDelegate <Second, First> dGeneric = ASecondRFirst; SampleGenericDelegate <Second, First> dGenericConversion = AFirstRSecond; SampleGenericDelegate <string> hahah = () => ""; SampleGenericDelegate <object> obj = hahah; Console.ReadKey(); }
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(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(); }
public static void Main() { SampleDelegate del = new SampleDelegate(Message1); //Declares del and initializes it to point to method Message1 del += Message2; //Now method Message2 also gets added to del. Del is now pointing to two methods, Message1 and Message2. So it is now a MultiCast Delegate del += Message3; //Method Message3 now also gets added to del del(); //Del invokes Message1, Message2 and Message3 in the same order as they were added /* * Output: * This is Message1 * This is Message2 * This is Message3 */ del -= Message1; //Method Message1 is now removed from Del. It no longer points to Message1 //Del invokes the two remaining Methods Message1 and Message2 in the same order del(); /* * New Output: * This is Message2 * This is Message3 */ del += Message4; //Method Message4 gets added to Del. The three Methods that Del oints to are in the order 1 -> Message2, 2 -> Message3, 3 -> Message4 //Del invokes the three methods in the same order in which they are present. del(); /* * New Output: * This is Message2 * This is Message3 * This is Message4 */ }
public static void Main() { DelegateClass dc = new DelegateClass(); SampleDelegate sd = new SampleDelegate(dc.DelegateMethod); sd(); }
static void Main(string[] args) { SampleDelegate SD1, SD2, SD3, SD4; SD1 = new SampleDelegate(SampleMethod1); SD2 = new SampleDelegate(SampleMethod2); SD3 = SD1 + SD2; SD3(); //This is a multicast delegate SD4 = SD1 + SD2 - SD1; //like the + sign , - sign can also be used to remove the delegates (here , SD4 = SD2 basically) SD4(); SampleDelegate SD5 = new SampleDelegate(SampleMethod1); SD5 += SampleMethod2; //Another way to multicast delegates SD5(); //Here , we don't need to create many instances of the delegate to make it multicast Console.ReadKey(); ReturningValueDelegate RVD; //Demonstrating return value RVD = new ReturningValueDelegate(ReturningValueMethod1); RVD += ReturningValueMethod2; int ReturnedValue = RVD(); Console.WriteLine("Returned Value is {0}", ReturnedValue); Console.ReadKey(); OutputParameterDelegate OPD; //Demonstrating output parameter OPD = new OutputParameterDelegate(OutputParameterMethod1); OPD += OutputParameterMethod2; int OutParameter = -1; OPD(out OutParameter); Console.WriteLine("Final output parameter is {0}", OutParameter); Console.ReadKey(); }
static void Main(string[] args) { DelegateClass delegateclass = new DelegateClass(); SampleDelegate sampledelegate = new SampleDelegate(delegateclass.DelegateHethod); sampledelegate(); }
private void btnStart_Click(object sender, EventArgs e) { if (sw == false) { //計測開始 myStopWatch.Start(); //表示更新タイマー開始 timer2.Start(); //スイッチon sw = true; //リセットボタン使用不可 btnReset.Enabled = false; //「スタート」だったボタンの表示を「ストップ」に変更 btnStart.Text = "ストップ"; } //スイッチがoff以外のとき(つまりはonのとき else { // 実行するデリゲートを作成 SampleDelegate sampleDelegate = new SampleDelegate(this.DelegatingMethod); //計測終了 //myStopWatch.Stop(); //表示固定 //timer2.Stop(); label3.Text = myStopWatch.Elapsed.ToString(); // 非同期で呼び出す IAsyncResult ar = sampleDelegate.BeginInvoke(@"C:\Test\TimeRecord.txt", label3.Text + System.Environment.NewLine , null, null); //スイッチoff sw = false; //リセットボタン使用可 btnReset.Enabled = true; //「ストップ」だったボタンの表示を「スタート」に変更 btnStart.Text = "スタート"; } }
static void DisplayResult(IAsyncResult result) { string format = (string)result.AsyncState; AsyncResult delegateResult = (AsyncResult)result; SampleDelegate delegateInstance = (SampleDelegate)delegateResult.AsyncDelegate; Console.WriteLine(format, delegateInstance.EndInvoke(result)); }
//在回调函数中儿取 static void DisplayResult(IAsyncResult result) { string format = (string)result.AsyncState; // 就是BeginInvoke的第三个参数 AsyncResult delegateResult = (AsyncResult)result; //传入的result总是一个AsyncResult实例,可用于获取原来的委托实例,从而可以调用EndInvoke SampleDelegate delegateInstance = (SampleDelegate)delegateResult.AsyncDelegate; Console.WriteLine(format, delegateInstance.EndInvoke(result)); //异步委托要调用EndInvoke有两个目的:1)获取近回值 2)防止内存泄漏 }
public static void Exercise2() { //Same effect as Exercise 1, but different syntax. SampleDelegate del = new SampleDelegate(SampleMethod1); del += SampleMethod2; del(); }
/// <summary> /// 処理を実行します。 /// </summary> public void Execute() { // // 匿名メソッドを構築して実行. // SampleDelegate d = delegate { Output.WriteLine("SAMPLE_ANONYMOUS_DELEGATE."); }; d.Invoke(); }
public static void Main(string[] args) { // Here is the Code that uses the delegate defined above. SampleDelegate sd = delegate(param) { Console.WriteLine("Called me using parameter - " + param); }; sd.Invoke("FromMain"); }
static void Main(string[] args) { SampleDelegate del = new SampleDelegate(SampleMethoOne); SampleDelegate del2 = new SampleDelegate(SampleMethoTwo); SampleDelegate del3 = del + del2; del += SampleMethoTwo; del(); del3(); }
static void Main(string[] args) { AnotherClass ac = new AnotherClass(); DelegateExample de = new DelegateExample(); SampleDelegate sd = new SampleDelegate(ac.Method1); sd += de.DellMethod; sd.Invoke(); Console.ReadLine(); }
static void Main(string[] args) { SampleDelegate del = new SampleDelegate(Method1); del += Method2; del += Method3; del += Method4; del(); Console.Read(); }
public void TestDelegateMultiCastsSingleInstance() { SampleDelegate del = TestMethod00; del += TestMethod01; del += TestMethod02; del += TestMethod03; del(); }
public static void Exercise1() { SampleDelegate del1, del2, del3, del4; del1 = new SampleDelegate(SampleMethod1); del2 = new SampleDelegate(SampleMethod2); del3 = new SampleDelegate(SampleMethod3); //Chain delegates together with the + symbol. del4 = del1 + del2 + del3; del4(); }
static void Main(string[] args) { SampleDelegate del = new SampleDelegate(SampleMethodOne); del += SampleMethodTwo; int DelegateOutputParameterValue = -1; del(out DelegateOutputParameterValue); Console.WriteLine("DelegateOutputParameterValue = {0}", DelegateOutputParameterValue); Console.ReadKey(); }
public static void Main(string[] args) { SampleDelegate del1, del2, del3, del4; del1 = new SampleDelegate(SampleMethodOne); del2 = new SampleDelegate(SampleMethodTwo); del3 = new SampleDelegate(SampleMethodThree); del4 = del1 + del2 + del3 - del2; del4(); }
public static void Main() { SampleDelegate del = new SampleDelegate(SampleMethodOne); del += SampleMethodTwo; int DelegateOutputParameterValue = -1; del(out DelegateOutputParameterValue); Console.WriteLine("DelegateOutputParameterValue = {0}", DelegateOutputParameterValue); }
public static void Main() { SampleDelegate del1, del2, del3, del4; del1 = new SampleDelegate(sampleMethod1); del2 = new SampleDelegate(sampleMethod2); del3 = new SampleDelegate(sampleMethod3); del4 = del1 + del2 + del3-del2; del4(); //This is the multicast delegate //Another way SampleDelegate del = new SampleDelegate(sampleMethod1); del += sampleMethod2; del += sampleMethod3; del -= sampleMethod1; del(); }
public void Init() { _type = typeof (string); _type2 = typeof (SomeValues); _dbnull = DBNull.Value; _assembly = typeof (SomeValues).Assembly; _intEnum = IntEnum.bbb; _byteEnum = ByteEnum.ccc; _bool = true; _bool2 = false; _byte = 254; _char = 'A'; _dateTime = new DateTime (1972,7,13,1,20,59); _decimal = (decimal)101010.10101; _double = 123456.6789; _short = -19191; _int = -28282828; _long = 37373737373; _sbyte = -123; _float = (float)654321.321; _ushort = 61616; _uint = 464646464; _ulong = 55555555; Point p = new Point(); p.x = 56; p.y = 67; object boxedPoint = p; long i = 22; object boxedLong = i; _objects = new object[] { "string", (int)1234, null , /*boxedPoint, boxedPoint,*/ boxedLong, boxedLong}; _strings = new string[] { "an", "array", "of", "strings","I","repeat","an", "array", "of", "strings" }; _ints = new int[] { 4,5,6,7,8 }; _intsMulti = new int[2,3,4] { { {1,2,3,4},{5,6,7,8},{9,10,11,12}}, { {13,14,15,16},{17,18,19,20},{21,22,23,24} } }; _intsJagged = new int[2][] { new int[3] {1,2,3}, new int[2] {4,5} }; _simples = new SimpleClass[] { new SimpleClass('a'),new SimpleClass('b'),new SimpleClass('c') }; _simplesMulti = new SimpleClass[2,3] {{new SimpleClass('d'),new SimpleClass('e'),new SimpleClass('f')}, {new SimpleClass('g'),new SimpleClass('j'),new SimpleClass('h')}}; _simplesJagged = new SimpleClass[2][] { new SimpleClass[1] { new SimpleClass('i') }, new SimpleClass[2] {null, new SimpleClass('k')}}; _almostEmpty = new object[2000]; _almostEmpty[1000] = 4; _emptyObjectArray = new object[0]; _emptyTypeArray = new Type[0]; _emptySimpleArray = new SimpleClass[0]; _emptyIntArray = new int[0]; _emptyStringArray = new string[0]; _emptyPointArray = new Point[0]; _doubles = new double[] { 1010101.101010, 292929.29292, 3838383.38383, 4747474.474, 56565.5656565, 0, Double.NaN, Double.MaxValue, Double.MinValue, Double.NegativeInfinity, Double.PositiveInfinity }; _sampleDelegate = new SampleDelegate(SampleCall); _sampleDelegate2 = new SampleDelegate(_simples[0].SampleCall); _sampleDelegate3 = new SampleDelegate(new SimpleClass('x').SampleCall); _sampleDelegateStatic = new SampleDelegate(SampleStaticCall); _sampleDelegateCombined = (SampleDelegate)Delegate.Combine (new Delegate[] {_sampleDelegate, _sampleDelegate2, _sampleDelegate3, _sampleDelegateStatic }); // This is to test that references are correctly solved _shared1 = new SimpleClass('A'); _shared2 = new SimpleClass('A'); _shared3 = _shared1; _falseSerializable = new FalseISerializable (2); }
static void Main() { Derived x = new Derived(); SampleDelegate factory = new SampleDelegate(x.CandidateAction); factory("test"); }
static void Main() { SampleDelegate test = new SampleDelegate(TimerDelegate.TimerTest); test(2); }