X hits on this document

1033 views

0 shares

0 downloads

0 comments

311 / 396

C# LANGUAGE SPECIFICATION

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 so cd3 is null

//cd3(70);// System.NullReferenceException thrown

cd3 -= cd1;// impossible removal is benign } }

As shown in the statement cd3 += cd1;, a delegate can be present in an invocation list multiple times. In this case, it is simply invoked once per occurrence. In an invocation list such as this, when that 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;, the delegate cd3 refers to an empty invocation list. Attempting to remove a delegate from an empty list (or to remove a non-existent delegate from a non-empty list) is not an error.

The output produced is:

C.M1: -1 C.M2: -2 C.M1: 10 C.M2: 10 C.M1: 20 C.M2: 20 C.M1: 20 C.M1: 30 C.M2: 30 C.M1: 30 C.M3: 30 C.M1: 40 C.M2: 40 C.M3: 40 C.M1: 50 C.M2: 50 C.M1: 60 C.M1: 60

298Copyright Microsoft Corporation 1999-2003. All Rights Reserved.

Document info
Document views1033
Page views1033
Page last viewedSat Dec 10 06:14:13 UTC 2016
Pages396
Paragraphs9401
Words133190

Comments