Jon Jagger
jon@jaggersoft.com
Table of Contents 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 Notes DownloadECMA-334 C# Language Specificationpreviousnextprevious at this levelnext at this level 22 Delegatesprevious at this levelnext at this level 22.1 Delegate declarations Paragraph 11 A delegate-declaration is a type-declaration (§16.5) that declares a new delegate type. delegate-declaration : attributesopt delegate-modifiersopt delegate return-type identifier ( formal-parameter-listopt ) ; delegate-modifiers : delegate-modifier delegate-modifiers delegate-modifier delegate-modifier : new public protected internal private 2 It is a compile-time error for the same modifier to appear multiple times in a delegate declaration. Paragraph 21 The new modifier is only permitted on delegates declared within another type, in which case it specifies that such a delegate hides an inherited member by the same name, as described in §17.2.2. Paragraph 31 The public, protected, internal, and private modifiers control the accessibility of the delegate type. 2 Depending on the context in which the delegate declaration occurs, some of these modifiers may not be permitted (§10.5.1). Paragraph 41 The delegate's type name is identifier. Paragraph 51 The optional formal-parameter-list specifies the parameters of the delegate, and return-type indicates the return type of the delegate. 2 A method and a delegate type are compatible if both of the following are true: Paragraph 61 Delegate types in C# are name equivalent, not structurally equivalent. [Note: However, instances of two distinct but structurally equivalent delegate types may compare as equal (§14.9.8). end note] 2 Specifically, two different delegate types that have the same parameter lists and return type are considered different delegate types. [Example: For example:
delegate 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 same return type and parameter list; however, these delegate types are two different types, so they are not interchangeable. The delegate types D1 and D2 are incompatible with the methods B.M2, B.M3, and B.M4, since they have different return types or parameter lists. end example] 3 The only way to declare a delegate type is via a delegate-declaration. 4 A delegate type is a class type that is derived from System.Delegate. 5 Delegate types are implicitly sealed, so it is not permissible to derive any type from a delegate type. 6 It is also not permissible to derive a non-delegate class type from System.Delegate. 7 Note that System.Delegate is not itself a delegate type; it is a class type from which all delegate types are derived. Paragraph 71 C# provides special syntax for delegate instantiation and invocation. 2 Except for instantiation, any operation that can be applied to a class or class instance can also be applied to a delegate class or instance, respectively. 3 In particular, it is possible to access members of the System.Delegate type via the usual member access syntax. Paragraph 81 The set of methods encapsulated by a delegate instance is called an invocation list. 2 When a delegate instance is created (§22.2) from a single method, it encapsulates that method, and its invocation list contains only one entry. 3 However, when two non-null delegate instances are combined, their invocation lists are concatenated-in the order left operand then right operand-to form a new invocation list, which contains two or more entries. Paragraph 91 Delegates are combined using the binary + (§14.7.4) and += operators (§14.13.2). 2 A delegate can be removed from a combination of delegates, using the binary (§14.7.5) -and -= operators (§14.13.2). 3 Delegates can be compared for equality (§14.9.8). [Example: The following example shows the instantiation of a number of delegates, and their corresponding invocation 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); // M1  
      D cd2 = new D(Test.M2); // m2  
      D cd3 = cd1 + cd2;    // M1 + M2  
      D cd4 = cd3 + cd1;     // M1 + M2 + M1  
      D 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 an invocation list of two methods, M1 and M2, in that order. cd4's invocation list contains M1, M2, and M1, in that order. 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]
{ JSL }
Jagger Software Ltd
Company # 4070126
VAT # 762 5213 42
Valid HTML 4.01Valid CSS