static void Main(string[] args) { /* * EXAMPLE 1 * [pole delegatu] */ /* * List<MyDelegate<double>> list = new List<MyDelegate<double>>(); * list.Add(MathOp.Add); * list.Add(MathOp.Div); * * double A = 10; * double B = 5; * double RESULT = 0; * * foreach (MyDelegate<double> del in list) * { * RESULT = del(A, B); * Console.WriteLine(string.Format("Result = {0}", RESULT)); * } */ /* * EXAMPLE 2 * [Multicast delegat - pomoci jednoho delegata zavolam vice metod] */ SecondDelegate del = MathOp.Add; del += MathOp.Div; del(); Console.ReadLine(); }
public static void Main232() { //Instantiating a delegate //Using the new keyword PerformCalculation performCal = new PerformCalculation(Add); //Using delegate inference PerformCalculation performCalculation = Add; SecondDelegate secondDelegate = CountEvenNumbers; //Invoking a delegate //Invoking Asynchronously int[] numArray = { 42, 44, 45, 46, 47, 48, 49, 50 }; secondDelegate.BeginInvoke(numArray, null, null); //Using the () operator performCal(40, 45); //Using the Invoke() method of the delegate class performCalculation.Invoke(40, 45); //End on the asynchronous invocation int sam = secondDelegate.EndInvoke(null); }
public static void Main(string[] args) { //Створюємо екземпляри делегатів та сповіщуємо їх з методами FirstDelegate firstDelegate = Msg1; SecondDelegate secondDelegate = Msg2; ThirdDelegate thirdDelegate = Msg3; //Анонімний метод FirstDelegate firstDelegateWithAnonymousMethod = delegate() { System.Diagnostics.Debug.WriteLine("This is message for delegate 1 with anonymous method"); }; //Лямбда-оператор та лямбда-вираз ThirdDelegate thirdDelegateWithLambdaOperatorAndExpression = (x) => System.Diagnostics.Debug.WriteLine("This is message for delegate 3 with parameter: " + x); //Викликаємо методи, сповіщені з делегатами firstDelegate(); System.Diagnostics.Debug.WriteLine("This is message for delegate 2 with return value: " + secondDelegate()); thirdDelegate("Test parameter"); firstDelegateWithAnonymousMethod(); string msg = "Parameter for Lambda expression"; thirdDelegateWithLambdaOperatorAndExpression(msg); //Створюємо екземпляр класу, що викликає подію Test test = new Test(); //Додаємо обробник подій EventMsg test.Notify += EventMsg; //Викликаємо метод, що викликає подію test.TestMethod(); CreateHostBuilder(args).Build().Run(); }
static CurrentTime() { SecondChanged += PrintToConsole; SecondChanged += PrintToFile; MinuteChanged += AlarmArray.Execute; }
public void UtilizeDelegate2() { var whatHorrorLiesHere = "test123"; SecondDelegate arrr = () => { Debug.Trace("UtilizeDelegate2 was used!" + whatHorrorLiesHere, 0); }; arrr(); }
static int Reduce(int[] arr, int acc_init, SecondDelegate del) { var acc = acc_init; foreach(var x in arr) { acc = del(acc, x); } return acc; }
static void Main(string[] args) { string zz = Console.ReadLine(); FirstDelegate del = new FirstDelegate(DelWithConstr); del(); SecondDelegate del2 = zz1 => DelWithOutConstructor(zz); del2(zz); Console.ReadKey(); }
static void Main(string[] args) { int n = 77777; int[] arr = { 10, 20, 30, 40, 50, 60, 70, 77, 80, 90, }; FirstDelegate firs = new FirstDelegate(FormMass); SecondDelegate second = new SecondDelegate(PrintMass); PrintMass(FormMass(n)); Console.WriteLine(); PrintMass(arr); Console.WriteLine(); Console.WriteLine($"{firs.Method}\t{firs.Target}"); Console.WriteLine($"{second.Method}\t{second.Target}"); }
public static void GetCurrentTime(object a) { WakeUPP[] massiv = (WakeUPP[])a; SoundPlayer sp = new SoundPlayer("C:\\Users\\Amargo\\Desktop\\Alarm01.wav"); SecondDelegate sc = new SecondDelegate(GetSecond); MinuteDelegate mn = new MinuteDelegate(GetMinute); Console.WriteLine(DateTime.Now); while (true) { DateTime NowTime = DateTime.Now; for (int i = 0; i < 100; i++) if (massiv[i]!= null) if (massiv[i].Hour == NowTime.Hour && massiv[i].Minute == NowTime.Minute) { sp.Play(); Console.WriteLine(massiv[i].Messager); string file_adress = "c:\\Users\\Amargo\\Desktop\\101\\" + "Будильник_" +i+".txt"; FileStream nowtimefile = new FileStream(file_adress, FileMode.Create); StreamWriter writer1 = new StreamWriter(nowtimefile); writer1.WriteLine("Будильник сработал в " + NowTime.Hour + " часов " + mn(NowTime) + " минут " + sc(NowTime) + " секунд") ; writer1.WriteLine("Текст будильника: " + massiv[i].Messager); writer1.Close(); Thread.Sleep(50000); sp.Stop(); } FileStream nowtimefile1 = new FileStream("c:\\Users\\Amargo\\Desktop\\101\\nowtime.txt", FileMode.Create); NowTime = DateTime.Now; StreamWriter writer = new StreamWriter(nowtimefile1); writer.Write("{0}", NowTime); writer.Close(); Thread.Sleep(1000); } }
static void Main(string[] args) { #region //DotNET 1.0 FirstDelegate first = new FirstDelegate(Square); Console.WriteLine(first(3)); //4.0 SecondDelegate second = Print; second += Display; Execute(second); //second.Invoke(); #endregion /* * Anonymous method */ //2 FirstDelegate df = delegate(int a) { return(a * a); }; Console.WriteLine(df(6)); //3.5 df = (a => a * a * a); Console.WriteLine(df(3)); Console.WriteLine(func("duong", 20)); //Func<int, int, int> f = ((a, b) => { // return a + b; //}); //Console.WriteLine(f(7,3)); Run(func, "phat", 100); Console.ReadLine(); }
public void SecondDelegateMethod(SecondDelegate method) { SecondDelegate secondDelegate = method; Console.WriteLine(secondDelegate("User name!")); }
static void Execute(SecondDelegate d) => d.Invoke();
static void Execute(SecondDelegate d) { d(); }