static void Main(string[] args) { //Formas de usar o delegate //1 DelCalculo somar = CalculationService.Somar; System.Console.WriteLine(somar(10, 20)); //2 - com o new DelCalculo somar2 = new DelCalculo(CalculationService.Somar); System.Console.WriteLine(somar2(10, 21)); //3 com o invoke DelCalculo somar3 = CalculationService.Somar; System.Console.WriteLine(somar3.Invoke(10, 22)); //Multi referencias com delegate MostrarNumeros mostrar = CalculationService.MostrarMenor; mostrar += CalculationService.MostrarMaior; mostrar += CalculationService.MostrarNumeroPar; mostrar += CalculationService.MostrarNumeroImpar; mostrar.Invoke(12, 13); var cal = new CalculationService(); DelContar c = cal.Contar; c(15); //Call back Del del = new Del(CalculationService.DelegateMethod); CalculationService.MethodWithCallback(10, 10, del); ReferenciaMetodo delRefMethod = CalculationService.DelegateMethod; CalculationService.ChamaDelegate("Passei o metodo ChamaDelegate", "para o Delegate Referencia Metodo", delRefMethod); Del d1 = CalculationService.DelegateMethod; Del d2 = CalculationService.DelegateMethod2; Del d3 = CalculationService.DelegateMethod2; Del d4 = d1 + d2 + d3; System.Console.WriteLine(d2 == d3); d4.Invoke("Invocando metodos"); }
public static void ChamaDelegate(string x, string y, ReferenciaMetodo del) { del.Invoke("Chamada de um delegate:" + x + y); }