Presentation is loading. Please wait.

Presentation is loading. Please wait.

From C++ to C# Part 5. Enums Similar to C++ Similar to C++ Read up section 1.10 of Spec. Read up section 1.10 of Spec.

Similar presentations


Presentation on theme: "From C++ to C# Part 5. Enums Similar to C++ Similar to C++ Read up section 1.10 of Spec. Read up section 1.10 of Spec."— Presentation transcript:

1 From C++ to C# Part 5

2 Enums Similar to C++ Similar to C++ Read up section 1.10 of Spec. Read up section 1.10 of Spec.

3 Delegate A delegate type represents references to methods with a particular parameter list and return type A delegate type represents references to methods with a particular parameter list and return type You can assign method references to delegate variables You can assign method references to delegate variables Delegate variables can be passed as parameters to methods Delegate variables can be passed as parameters to methods

4 Delegate vs Function Pointers Similar to Function pointers Similar to Function pointers But delegates are object-oriented and type safe But delegates are object-oriented and type safe

5 Delegate example delegate double Function(double x); delegate double Function(double x); Declares Function as a delegate type which takes in one parameter of type double and returns double. Declares Function as a delegate type which takes in one parameter of type double and returns double. Function f; Function f; Declares f as a variable of Function delegate type Declares f as a variable of Function delegate type f can reference any method which has appropriate argument signature f can reference any method which has appropriate argument signature

6 … Delegate example Method Apply() takes in a double array and a Function delegate type parameter Method Apply() takes in a double array and a Function delegate type parameter Apply() then invokes method referenced by Function for each element of the double array and stores the return value (for each element) in a new double array Apply() then invokes method referenced by Function for each element of the double array and stores the return value (for each element) in a new double array Finally Apply() returns the new double array Finally Apply() returns the new double array

7 … Delegate example static double[] Apply(double[] a, Function f) { static double[] Apply(double[] a, Function f) { double[] result = new double[a.Length]; double[] result = new double[a.Length]; for (int i = 0; i < a.Length; i++) for (int i = 0; i < a.Length; i++) result[i] = f(a[i]); result[i] = f(a[i]); return result; return result; }

8 … Delegate example Apply method is passed a delegate type parameter f which references appropriate methods. Apply method is passed a delegate type parameter f which references appropriate methods. f(a[i]); invokes the method referenced by delegate type parameter f f(a[i]); invokes the method referenced by delegate type parameter f

9 … Delegate example Method Square() has argument signature matching Function delegate type Method Square() has argument signature matching Function delegate type static double Square(double x) { static double Square(double x) { return x * x; return x * x; }

10 … Delegate example … So Apply() method can be called using a Function delegate variable initialized to reference method Square() So Apply() method can be called using a Function delegate variable initialized to reference method Square() double[] squares = Apply(a, double[] squares = Apply(a, new Function(Square)); new Function(Square)); Note that a is an array of double (input data) Note that a is an array of double (input data)

11 Delegate example code See 1.11.cs See 1.11.cs Delegate can reference either a static method or an instance method Delegate can reference either a static method or an instance method Delegate does not know or care about the class of the method it references Delegate does not know or care about the class of the method it references The method should just have the same argument signature The method should just have the same argument signature

12 Multi-cast Delegate A Delegate can reference multiple methods (of matching argument signature) A Delegate can reference multiple methods (of matching argument signature) See 15.3.cs See 15.3.cs Delegate variable supports =, += and -= operations Delegate variable supports =, += and -= operations

13 Event vs Delegate The EventHandler of.Net Framework (seen in list.cs) is defined as follows. The EventHandler of.Net Framework (seen in list.cs) is defined as follows. public delegate void EventHandler( public delegate void EventHandler( Object sender, Object sender, EventArgs e EventArgs e )

14 Event vs Delegate So in list.cs we could have a delegate field: So in list.cs we could have a delegate field: public EventHandler Changed; public EventHandler Changed; Instead of Instead of public event EventHandler Changed; public event EventHandler Changed; Adding references to delegate type variables is same as for event. (=, +=, -=) Adding references to delegate type variables is same as for event. (=, +=, -=) Invoking the method references is also same as for event. Invoking the method references is also same as for event.

15 … Event vs. Delegate “event” keyword is actually just a modifier for the delegate type “event” keyword is actually just a modifier for the delegate type It encapsulates the delegate field and allows lesser number of operations on the ‘event’ field. It encapsulates the delegate field and allows lesser number of operations on the ‘event’ field. += or -= are allowed on events += or -= are allowed on events But invoking the (public) event like Changed(…,…) is NOT ALLOWED for class users. But invoking the (public) event like Changed(…,…) is NOT ALLOWED for class users.

16 … Event vs. Delegate In list.cs the invocation of the event is done by the protected methods OnChanged() which is called by the public Add() method. In list.cs the invocation of the event is done by the protected methods OnChanged() which is called by the public Add() method. Thus firing the Changed event (i.e. invoking Changed(…,…)) is limited to list class and classes derived from list. Thus firing the Changed event (i.e. invoking Changed(…,…)) is limited to list class and classes derived from list. This is a significant protection from inadvertent or malicious firing of the event from class user code. This is a significant protection from inadvertent or malicious firing of the event from class user code.

17 … Event vs. Delegate But invoking the (public) delegate field IS ALLOWED for class users But invoking the (public) delegate field IS ALLOWED for class users And so no protection is provided against class users inadvertently or maliciously invoking the delegate method reference(s). And so no protection is provided against class users inadvertently or maliciously invoking the delegate method reference(s). There may be other differences also between Events And Delegates (e.g. thread safety) There may be other differences also between Events And Delegates (e.g. thread safety) But for our purposes this knowledge is enough. But for our purposes this knowledge is enough.

18 Attributes Self Study: Section 1.12 Self Study: Section 1.12

19 Observation10 Write your observations about the programs studied today (1.11.cs and 15.3.cs) Write your observations about the programs studied today (1.11.cs and 15.3.cs)


Download ppt "From C++ to C# Part 5. Enums Similar to C++ Similar to C++ Read up section 1.10 of Spec. Read up section 1.10 of Spec."

Similar presentations


Ads by Google