private static void FuncDemo() { Matematik matematik = new Matematik(); Func <int, int, int> func = matematik.Carp; Console.WriteLine(func.Invoke(4, 5)); Func <int> randomNumber = delegate() { var random = new Random(); int randomNum = random.Next(1, 20); return(randomNum); }; Console.WriteLine(randomNumber.Invoke()); DelegateCalculate delegateCalculate = matematik.Bol; }
static void Main(string[] args) { Dictionary <BaseFigure, DelegateCalculate> calculation = new Dictionary <BaseFigure, DelegateCalculate>(); var square = new Square(10); var circle = new Circle(12); var rectangle = new Rectangle(5, 10); var triangle = new Triangle(3, 5, 8, 12); DelegateCalculate squareCalculate = square.GetArea; squareCalculate += square.GetPerimetr; DelegateCalculate circleCalculate = circle.GetArea; circleCalculate += circle.GetPerimetr; DelegateCalculate rectangleCalculate = rectangle.GetArea; rectangleCalculate += rectangle.GetPerimetr; DelegateCalculate triangleCalculate = triangle.GetArea; triangleCalculate += triangle.GetPerimetr; calculation.Add(square, squareCalculate); calculation.Add(circle, circleCalculate); calculation.Add(rectangle, rectangleCalculate); calculation.Add(triangle, triangleCalculate); calculation[square](); calculation[circle](); calculation[rectangle](); calculation[triangle](); Console.WriteLine(square.ToString()); Console.WriteLine(circle.ToString()); Console.WriteLine(rectangle.ToString()); Console.WriteLine(triangle.ToString()); Console.ReadKey(); }
private static void DelegateDemo() { CustomerManager customerManager = new CustomerManager(); //CustomerManeger classından nesne oluşturuldu. DelegateMessenger delegateMessenger = customerManager.SendMessage; //Bu nesneye ait metotlar oluşturulan delegateye atandı. delegateMessenger += customerManager.ShowAlert; //delegate ye metot eklendi. delegateMessenger(); //delegate aktif hale getirildi. Matematik matematikIslem = new Matematik(); DelegateMath delegateMath = matematikIslem.Topla; delegateMath += matematikIslem.Cikarma; delegateMath(4, 3); DelegateCalculate delegateCalculate = matematikIslem.Bol; result = delegateCalculate(5, 3); Console.WriteLine(result); delegateCalculate += matematikIslem.Carp; Console.WriteLine(delegateCalculate(result, 3)); }