C # provides Special Syntax for Invoking a delegate .when a non-null delegate
Instance whose invocation list
Contains One Entry, Is Invoked, IT IT IT IT ITHE SAME
Arguments it Was Given, And Returns the
Same value as the refered to method. (see §14.5.5.2 for details
INFORMATION ON DELEGATE INVOCATION.) IF AN
Exception Occurs During The Invocation of Such a delegate, And That
Exception is not caught within the method That
Was Invoked, The Search for An Exception Catch Clause Continues in The
Method That Called The Delegate, as if That
Method Had Directly Called The Method to Which That Delegate ReferRed.
INVOCATION OF A DELEGATE Instance Whose Invocation List Contains Multiple
Entries, Proceeds by Invoking Each of
The Methods in The Invocation List, Synchronously, in Order. Each Method So
Called is passed the Same set of
ARGUMENTS As Was Given to the delegate Instance. If Such a delegate
Invocation Includes Reference Parameters
(17.5.1.2), Each Method Invocation Will Occur with a reason to the
Same variable; changes to this variable by
One Method in The Invocation List Will Be Visible To Methods Further Down
The Invocation List. if the delegate
Invocation Includes Output Parameters or a return value, Their Final Value
Will Come from The Invocation of the Last
Delegate in the list. if an exception Occurs during processing of the
Invocation of Such a Delegate, and this
Exception is not caught within the method That Was Invoked, The Search for
An Exception Catch Clause Continues in
THE METHOD THAT Called The Delegate, And Any Methods Further Down The
Invocation List area not invoked.
Attempting to invoke a delegate Instance Whose Value Is Null Results in A
Exception of TypeSystem.nullReferenceException.
[EXAMPLE: The Following Example Shows How To Instantiate, Combine, Remove,
And Invoke Delegates:
C # language specification
300
Using system;
Delegate Void D (int X);
Class test
{
Public Static Void M1 (INT I) {
Console.writeline ("Test.m1:" i);
}
Public Static Void M2 (INT I) {
Console.writeLine ("Test.m2:" i);
}
Public Void M3 (INT I) {
Console.WriteLine ("Test.m3:" i);
}
}
Class Demo
{
Static void main () {
D CD1 = New D (Test.m1);
CD1 (-1); // Call M1
D CD2 = New D (Test.m2);
CD2 (-2); // Call M2
D CD3 = CD1 CD2;
CD3 (10); // Call M1 Then M2
CD3 = CD1;
CD3 (20); // Call M1, M2, THEN M1
Test T = New Test ();
D CD4 = New D (t.m3);
CD3 = CD4;
CD3 (30); // Call M1, M2, M1, THEN M3
CD3 - = CD1; // Remove Last M1
CD3 (40); // Call M1, M2, THEN M3
CD3 - = CD4;
CD3 (50); // Call M1 THEN M2
CD3 - = CD2;
CD3 (60); // Call M1
CD3 - = CD2; // Impossible Removal IS Benign
CD3 (60); // Call M1
CD3 - = CD1; // Invocation List is EMPTY
// CD3 (70); // system.nullreferenceException thrown
CD3 - = CD1; // Impossible Removal IS Benign
}
}
AS Shown In The Statement CD3 = CD1 ;, A delegate Can Be Present in A
Invocation List Multiple Times. in this
Case, IT IS SIMPLY INVOKED ONCE PER OCCURRENCE. INVOCATION LIST SUCH
As this, when it delegate is removed,
The Last Occurrence In The Invocation List is The One Actually Removed.
IMMEDIATELY PRIOR TO THE EXECUTION OF THE FINAL STATEMENT, CD3 - = CD1;
Delegate CD3 Refers to an Empty
Invocation List. Attempting to Remove a delegate from an Empty List (Or To
REMOVE A NON-EXISTENT DELEGATE AON-EMPTY LIST) IS Not An Error.
The Output Productued IS:
Test.m1: -1
Test.m2: -2
Test.m1: 10
Test.m2: 10
Test.m1: 20
Test.m2: 20
Test.m1: 20
Chapter 22 Delegates
301
Test.m1: 30
TEST.M2: 30
Test.m1: 30
TEST.M3: 30
Test.m1: 40
Test.m2: 40
Test.m3: 40
Test.m1: 50
Test.m2: 50
Test.m1: 60
Test.m1: 60
End example]