from c++ to c# part 5. enums similar to c++ similar to c++ read up section 1.10 of spec. read up...

19
From C++ to C# Part 5

Upload: naomi-chambers

Post on 06-Jan-2018

214 views

Category:

Documents


0 download

DESCRIPTION

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

TRANSCRIPT

Page 1: 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

From C++ to C#

Part 5

Page 2: 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

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

Page 3: 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

Delegate A delegate type represents references to

methods with a particular parameter list and return type

You can assign method references to delegate variables

Delegate variables can be passed as parameters to methods

Page 4: 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

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

Page 5: 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

Delegate example delegate double Function(double x); Declares Function as a delegate type which

takes in one parameter of type double and returns double.

Function f; Declares f as a variable of Function delegate

type f can reference any method which has

appropriate argument signature

Page 6: 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

… Delegate example 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

Finally Apply() returns the new double array

Page 7: 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

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

Page 8: 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

… Delegate example 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

Page 9: 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

… Delegate example Method Square() has argument signature

matching Function delegate type static double Square(double x) { return x * x; }

Page 10: 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

… Delegate example … So Apply() method can be called using a

Function delegate variable initialized to reference method Square()

double[] squares = Apply(a, new Function(Square)); Note that a is an array of double (input data)

Page 11: 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

Delegate example code See 1.11.cs 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 The method should just have the same

argument signature

Page 12: 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

Multi-cast Delegate A Delegate can reference multiple methods (of

matching argument signature) See 15.3.cs Delegate variable supports =, += and -=

operations

Page 13: 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

Event vs Delegate The EventHandler of .Net Framework (seen in

list.cs) is defined as follows. public delegate void EventHandler( Object sender, EventArgs e )

Page 14: 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

Event vs Delegate So in list.cs we could have a delegate field: public EventHandler Changed; Instead of public event EventHandler Changed; Adding references to delegate type variables is

same as for event. (=, +=, -=) Invoking the method references is also same as

for event.

Page 15: 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

… Event vs. Delegate “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.

+= or -= are allowed on events But invoking the (public) event like

Changed(…,…) is NOT ALLOWED for class users.

Page 16: 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

… 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.

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.

Page 17: 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

… Event vs. Delegate 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).

There may be other differences also between Events And Delegates (e.g. thread safety)

But for our purposes this knowledge is enough.

Page 18: 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

Attributes Self Study: Section 1.12

Page 19: 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

Observation10 Write your observations about the programs

studied today (1.11.cs and 15.3.cs)