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.

<strong>C#</strong> LANGUAGE SPECIFICATIONdelegate int D1(int i, double d);class A{public static int M1(int a, double b) {…}}class B{delegate int D2(int c, double d);public static int M1(int f, double g) {…}public static void M2(int k, double l) {…}public static int M3(int g) {…}public static void M4(int g) {…}}The delegate types D1 and D2 are both compatible with the methods A.M1 and B.M1, since they have the samereturn type and parameter list; however, these delegate types are two different types, so they are notinterchangeable. The delegate types D1 and D2 are incompatible with the methods B.M2, B.M3, and B.M4, sincethey have different return types or parameter lists. end example]The only way to declare a delegate type is via a delegate-declaration. A delegate type is a class type that isderived from System.Delegate. Delegate types are implicitly sealed, so it is not permissible to derive anytype from a delegate type. It is also not permissible to derive a non-delegate class type from System.Delegate.System.Delegate is not itself a delegate type; it is a class type from which all delegate types are derived.<strong>C#</strong> provides special syntax for delegate instantiation and invocation. Except for instantiation, any operation thatcan be applied to a class or class instance can also be applied to a delegate class or instance, respectively. Inparticular, it is possible to access members of the System.Delegate type via the usual member access syntax.The set of methods encapsulated by a delegate instance is called an invocation list. When a delegate instance iscreated (§22.2) from a single method, it encapsulates that method, and its invocation list contains only one entry.However, when two non-null delegate instances are combined, their invocation lists are concatenated—in theorder left operand then right operand—to form a new invocation list, which contains two or more entries.Delegates are combined using the binary + (§14.7.4) and += operators (§14.13.2). A delegate can be removedfrom a combination of delegates, using the binary - (§14.7.5) and -= operators (§14.13.2). Delegates can becompared for equality (§14.9.8).[Example: The following example shows the instantiation of a number of delegates, and their correspondinginvocation lists:delegate void D(int x);class Test{public static void M1(int i) {…}public static void M2(int i) {…}}class Demo{static void Main() {D cd1 = new D(Test.M1); // M1D cd2 = new D(Test.M2); // m2D cd3 = cd1 + cd2; // M1 + M2D cd4 = cd3 + cd1; // M1 + M2 + M1D cd5 = cd4 + cd3; // M1 + M2 + M1 + M1 + M2}}When cd1 and cd2 are instantiated, they each encapsulate one method. When cd3 is instantiated, it has aninvocation list of two methods, M1 and M2, in that order. cd4’s invocation list contains M1, M2, and M1, in thatorder. Finally, cd5’s invocation list contains M1, M2, M1, M1, and M2, in that order.For more examples of combining (as well as removing) delegates, see §22.3. end example]298

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

Saved successfully!

Ooh no, something went wrong!