Presentation is loading. Please wait.

Presentation is loading. Please wait.

33) static void Main() { Action operations = MathOperations.MultiplyByTwo; operations += MathOperations.Square; ProcessAndDisplayNumber(operations, 2.0);

Similar presentations


Presentation on theme: "33) static void Main() { Action operations = MathOperations.MultiplyByTwo; operations += MathOperations.Square; ProcessAndDisplayNumber(operations, 2.0);"— Presentation transcript:

1 33) static void Main() { Action operations = MathOperations.MultiplyByTwo; operations += MathOperations.Square; ProcessAndDisplayNumber(operations, 2.0); ProcessAndDisplayNumber(operations, 7.94); ProcessAndDisplayNumber(operations, 1.414); Console.WriteLine() ; } 34) MulticastDelegate ProcessAndDisplayNumber вызван со значением value = 2 Умножение на 2: 2 дает 4 Возведение в квадрат: 2 дает 4 ProcessAndDisplayNumber вызван со значением value =7.94 Умножение на 2: 7.94 дает 15.88 Возведение в квадрат: 7.94 дает 63.0436 ProcessAndDisplayNumber вызван со значением value = 1.414 Умножение на 2: 1.414 дает 2.828 Возведение в квадрат: 1.414 дает 1.999396

2 35) using System; namespace Wrox.ProCSharp.Delegates { class Program { static void One() { Console.WriteLine("One"); throw new Exception("Ошибка в One"); } static void Two() { Console.WriteLine("Two"); }

3 36) static void Main() { Action d1 = One; d1 += Two; try { d1(); } catch (Exception) { Console.WriteLine("Произошло исключение"); } 37) One Произошло исключение

4 38) static void Main() { Action d1 = One; d1 += Two; Delegate[] delegates = d1.GetInvocationList(); foreach (Action d in delegates) { try { d() ; } catch (Exception) { Console.WriteLine("Произошло исключение"); } 39) One Произошло исключение Two

5 40) using System; namespace Wrox.ProCSharp.Delegates { class Program { static void Main() { string mid = ", средняя часть,"; Func anonDel = delegate(string param) { param += mid; param += " а это добавлено к строке." ; return param; }; Console.WriteLine(anonDel("Начало строки")); }

6 41) using System; namespace Wrox.ProCSharp.Delegates { class Program { static void Main() { string mid = ", средняя часть,"; Func lambda = param => { param += mid; param += " а это добавлено к строке."; return param; }; Console.WriteLine(lambda("Начало строки”)); }

7 42) Func oneParam = s => String.Format( "изменение регистра {0} ", s.ToUpper()); Console.WriteLine(oneParam("test")); 43) Func twoParams = (x, y) => x * y; Console.WriteLine(twoParams (3, 2)); 44) Func twoParamsWithTypes = (double x, double y) => x * y; Console.WriteLine(twoParamsWithTypes(4, 2)); 45) Func square = х => х * х; 46) Func square = х => { return х * х; } 47) Func lambda = param => { param += mid; param += " а это добавлено к строке." ; return param; };

8 48) int someVal = 5; Func f = x => x + someVal; 49) someVal = 7; Console.WriteLine(f(3)) ; 50) public class AnonymousClass { private int someVal; public AnonymousClass(int someVal) { this.someVal = someVal; } public int AnonymousMethod(int x) { return x + someVal; }

9 51) using System; namespace Wrox.ProCSharp.Delegates { public class CarInfoEventArgs: EventArgs { public CarInfoEventArgs(string car) { this.Car = car; } public string Car {get; private set;} } public class CarDealer { public event EventHandler NewCarInfo; public void NewCar(string car) { Console.WriteLine("CarDealer, новый автомобиль {0}", car); if (NewCarlnfo != null) { NewCarlnfo(this, new CarlnfoEventArgs (car) ) ; }

10 52) public event EventHandler NewCarlnfo; 53) public delegate void EventHandler (object sender, TEventArgs e) where TEventArgs: EventArgs 54) private delegate EventHandler newCarInfo; public event EventHandler NewCarlnfo { add { newCarlnfo += value; } remove { newCarInfo = value; }

11 55) public void NewCar(string car) { Console.WriteLine("CarDealer, новый автомобиль {0}", car); if (NewCarlnfo != null) { NewCarlnfo(this, new CarlnfoEventArgs(car)); } 56) using System; namespace Wrox.ProCSharp.Delegates { public class Consumer { private string name; public Consumer(string name) { this.name = name; } public void NewCarIsHere(object sender, CarInfoEventArgs e) { Console.WriteLine ("{0}: автомобиль {1} является новым", name, e.Car); }

12 57) namespace Wrox.ProCSharp.Delegates { class Program { static void Main() { var dealer = new CarDealer(); var michael = new Consumer("Michael"); dealer.NewCarInfo += michael.NewCarIsHere; dealer.NewCar("Mercedes"); var nick = new Consumer ("Nick"); dealer.NewCarInfo += nick.NewCarIsHere; dealer.NewCar("Ferrari"); dealer.NewCarInfo = michael.NewCarlsHere; dealer.NewCar("Toyota"); }

13 58) CarDealer, новый автомобиль Mercedes Michael: автомобиль Mercedes является новым CarDealer, новый автомобиль Ferrari Michael: автомобиль Ferrari является новым Nick: автомобиль Ferrari является новым CarDealer, новый автомобиль Toyota Nick: автомобиль Toyota является новым 59) using System.Windows; namespace Wrox.ProCSharp.Delegates { public class WeakCarInfoEventManager: WeakEventManager { public static void AddListener(object source, IWeakEventListener listener) { CurrentManager.ProtectedAddListener(source, listener); } public static void RemoveListener(object source, IWeakEventListener listener) { CurrentManager.ProtectedRemoveListener(source, listener); }

14 public static WeakCarInfoEventManager CurrentManager { get { WeakCarInfoEventManager manager = GetCurrentManager(typeof(WeakCarInfoEventManager)) as WeakCarInfoEventManager; if (manager == null) { manager = new WeakCarInfoEventManager(); SetCurrentManager(typeof(WeakCarlnfoEventManager), manager); } return manager; } protected override void StartListening(object source) { (source as CarDealer).NewCarInfo += CarDealer_NewCarInfo; }

15 void CarDealer_NewCarInfo(object sender, CarInfoEventArgs e) { DeliverEvent(sender, e); } protected override void StopListening(object source) { (source as CarDealer).NewCarInfo = CarDealer_NewCarInfo; }

16 60) using System; using System.Windows; namespace Wrox.ProCSharp.Delegates { public class Consumer: IWeakEventListener { private string name; public Consumer(string name) { this.name = name; } public void NewCarIsHere(object sender, CarInfoEventArgs e) { Console.WriteLine ("{0} : автомобиль {1} является новым", name, e.Car); } bool IWeakEventListener.ReceiveWeakEvent(Type manager Type, object sender, EventArgs e) { NewCarIsHere(sender, e as CarInfoEventArgs); return true; }

17 61) static void Main() { var dealer = new CarDealer() ; var michael = new Consumer("Michael") ; WeakCarInfoEventManager.AddListener(dealer, michael); dealer.NewCar("Mercedes") ; var nick = new Consumer("Nick") ; WeakCarInfoEventManager.AddListener(dealer, nick); dealer.NewCar("Ferrari"); WeakCarInfoEventManager.RemoveListener(dealer, michael); dealer.NewCar("Toyota”); }


Download ppt "33) static void Main() { Action operations = MathOperations.MultiplyByTwo; operations += MathOperations.Square; ProcessAndDisplayNumber(operations, 2.0);"

Similar presentations


Ads by Google