1/21
Handout 01
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
Delegate
Is a class in .NET that encapsulates a method.
It is also a reference type data type that holds a reference method.
This class can call any method as long as its signature matches.
delegate keyword
Is used in declaring a delegate on a class.
signature methods
Specify what kind of delegate to be declared
Read-Only-Text
Read-Only-Text:
To declare a delegate, identify what access modifier should be used first. Then, call the delegate keyword, the return type, the desired name of the delegate, and the parameters inside the open and close parentheses ().
public delegate int mathOp(int x, int y);
new keyword
It is used to instantiate the delegate that is associated with the method and supposed to refer on the object.
Read-Only-Text
Read-Only-Text: Use new keyword to instantiate the delegate.
GetAnswer mdAdd = new GetAnswer(Formula.getSum);
GetAnswer mdAdd = Formula.Addition;
double
What return type is declared in the given example?
public delegate double Print();
public
What access modifier is declared in the givenexample?
public delegate double BasicMath (double a, double b);
What delegate name is declared in the given example?
public delegate void Print (int value);
getName
What is the target method’s name?
Print p1 = new Print(getName);
addition
What is the delegate object’s name?
Print addition = new Print(getSum);
What is the declared delegate name?
Print p1= new Print(getSum);
Generic delegates
Are not bound to any specific type. These can reference a method that returns and takes parameters of different types.
Read-Only-Text
Read-Only-Text:
declaring a genericdelegate:
public delegate X DisplayOutput(X arg);
X
A generic type parameter (can be int, string, etc.)
Event
Is a member of a class that is fired whenever a specific action takes place.
Read-Only-Text
Read-Only-Text:
When an event occurs, the method that has been created and registered to this event is automatically invoked.
Event handlers
The methods that are invoked when an event occurs
+=
This operator allows adding an event to the class.
Read-Only-Text
Read-Only-Text: To declare an event
accessModifier event delegateName eventName;
Read-Only-Text
Read-Only-Text: To register an event handler.
eventName += new DelegateName (evHandler)
Read-Only-Text
Read-Only-Text: To invoke an event:
eventName();
or
eventName?.Invoke();