13.07.2015 Views

C# Language Specification - Willy .Net

C# Language Specification - Willy .Net

C# Language Specification - Willy .Net

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 22 Delegates22.2 Delegate instantiationAn instance of a delegate is created by a delegate-creation-expression (§14.5.10.3). The newly created delegateinstance then refers to either:• The static method referenced in the delegate-creation-expression, or• The target object (which cannot be null) and instance method referenced in the delegate-creationexpression,or• Another delegate[Example: For example:delegate void D(int x);class Test{public static void M1(int i) {…}public void M2(int i) {…}}class Demo{static void Main() {D cd1 = new D(Test.M1); // static methodTest t = new Test();D cd2 = new D(t.M2); // instance methodD cd3 = new D(cd2); // another delegate}}end example]Once instantiated, delegate instances always refer to the same target object and method. [Note: Remember, whentwo delegates are combined, or one is removed from another, a new delegate results with its own invocation list;the invocation lists of the delegates combined or removed remain unchanged. end note]22.3 Delegate invocation<strong>C#</strong> provides special syntax for invoking a delegate. When a non-null delegate instance whose invocation listcontains one entry, is invoked, it invokes the one method with the same arguments it was given, and returns thesame value as the referred to method. (See §14.5.5.2 for detailed information on delegate invocation.) If anexception occurs during the invocation of such a delegate, and that exception is not caught within the method thatwas invoked, the search for an exception catch clause continues in the method that called the delegate, as if thatmethod 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 ofthe methods in the invocation list, synchronously, in order. Each method so called is passed the same set ofarguments 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 reference to the same variable; changes to that variable byone method in the invocation list will be visible to methods further down the invocation list. If the delegateinvocation includes output parameters or a return value, their final value will come from the invocation of the lastdelegate in the list. If an exception occurs during processing of the invocation of such a delegate, and thatexception is not caught within the method that was invoked, the search for an exception catch clause continues inthe method that called the delegate, and any methods further down the invocation list are not invoked.Attempting to invoke a delegate instance whose value is null results in an exception of typeSystem.NullReferenceException.[Example: The following example shows how to instantiate, combine, remove, and invoke delegates:299

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!