from c++ to c# part 5. enums similar to c++ similar to c++ read up section 1.10 of spec. read up...
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 methodsTRANSCRIPT
From C++ to C#
Part 5
Enums Similar to C++ 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
Delegate vs Function Pointers Similar to Function pointers But delegates are object-oriented and type safe
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
… 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
… 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; }
… 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
… Delegate example Method Square() has argument signature
matching Function delegate type static double Square(double x) { return x * x; }
… 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)
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
Multi-cast Delegate A Delegate can reference multiple methods (of
matching argument signature) See 15.3.cs Delegate variable supports =, += and -=
operations
Event vs Delegate The EventHandler of .Net Framework (seen in
list.cs) is defined as follows. public delegate void EventHandler( Object sender, EventArgs e )
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.
… 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.
… 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.
… 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.
Attributes Self Study: Section 1.12
Observation10 Write your observations about the programs
studied today (1.11.cs and 15.3.cs)