BCL mscorlib [00 00 00 00 00 00 00 00 04 00 00 00 00 00 00 00 ] 1.0.5000.0 2.0.0.0 4.0.0.0 System.Object System.ICloneable System.Runtime.Serialization.ISerializable System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.AutoDual) System.Runtime.InteropServices.ComVisible(true)

A class used to create types that invoke methods.

Delegate types derive from the class. The declaration of a delegate type establishes a contract that specifies the signature of one or more methods. For an example of a delegate type declaration, see the examples at the end of this topic.

Delegate types are implicitly sealed: it is not permissible to derive a new type from a delegate type. The class is not considered a delegate type; it is a class used to derive delegate types.

For information on subclassing the Delegate class, see Partition II of the CLI Specification.

A delegate is an instance of a delegate type. A non-null delegate references an , which is made up of one or more entries. Each entry consists of a pair of values: a non-null method, and a corresponding object, called the . If the method is static, the corresponding target is , otherwise the target is the instance on which the method is to be called.

The signature of each method in the invocation list is required to exactly match the signature specified by the delegate's type.

When a delegate is invoked, the methods in the corresponding invocation list are invoked in the order in which they appear in that list. A delegate attempts to invoke every method in its invocation list, with duplicate methods being invoked once for each occurrence in that list.

Delegates are immutable; once created, the invocation list of a delegate does not change. Combining operations, such as and , cannot alter existing delegates. Instead, such operations result in the return of either a new delegate that contains the results of the operation, an existing delegate, or the null value. A combining operation returns the null value when the result of the operation is an empty invocation list. A combining operation returns an existing delegate when the requested operation has no effect (for example, if an attempt is made to remove a nonexistent entry).

If an invoked method throws an exception, the method stops executing and the exception is passed back to the caller of the delegate. The delegate does not continue invoking methods from its invocation list. Catching the exception in the caller does not alter this behavior. It is possible that non-standard methods that implement combining operations allow the creation of delegates with different behavior. When this is the case, the non-standard methods are required to specify the behavior.

When the signature of the methods invoked by a delegate includes a return value, the delegate returns the return value of the last element in the invocation list. When the signature includes a parameter that is passed by reference, the final value of the parameter is the result of every method in the invocation list executing sequentially and updating the parameter's value. For an example that demonstrates this behavior, see Example 2.

The following example creates two delegates. The first delegate invokes a static method, and the second invokes an instance method on a target object.

`using System; public delegate string DelegatedMethod(string s); class MyClass { public static string StaticMethod(string s) { return ("Static method Arg=" + s); } public string InstanceMethod(string s) { return ("Instance method Arg=" + s); } } class TestClass { public static void Main() { MyClass myInstance = new MyClass(); //Create delegates from delegate type DelegatedMethod. DelegatedMethod delStatic = new DelegatedMethod(MyClass.StaticMethod); DelegatedMethod delInstance = new DelegatedMethod(myInstance.InstanceMethod); //Invoke the methods referenced by the delegates. Console.WriteLine (delStatic("Call 1")); Console.WriteLine (delInstance ("Call 2")); } }`

The output is

Static method Arg=Call 1

Instance method Arg=Call 2

The following example shows the return value and the final value of a parameter that is passed by reference to a delegate that invokes multiple methods.

`using System; class MyClass { public int Increment(ref int i) { Console.WriteLine("Incrementing {0}",i); return (i++); } public int Negate(ref int i) { Console.WriteLine("Negating {0}",i); i = i * -1; return i; } } public delegate int DelegatedMethod(ref int i); class TestClass { public static void Main() { MyClass myInstance = new MyClass(); DelegatedMethod delIncrementer = new DelegatedMethod(myInstance.Increment); DelegatedMethod delNegater = new DelegatedMethod(myInstance.Negate); DelegatedMethod d = (DelegatedMethod) Delegate.Combine(delIncrementer, delNegater); int i = 1; Console.WriteLine("Invoking delegate using ref value {0}",i); int retvalue = d(ref i); Console.WriteLine("After Invoking delegate i = {0} return value is {1}",i, retvalue); } }`

The output is

Invoking delegate using ref value 1

Incrementing 1

Negating 2

After Invoking delegate i = -2 return value is -2

- Constructor
  To be added.
  To be added.
  To be added.
  To be added.
  1.0.5000.0
  2.0.0.0
  4.0.0.0
- Constructor
  To be added.
  To be added.
  To be added.
  To be added.
  1.0.5000.0
  2.0.0.0
  4.0.0.0
- Method
  System.Object

  Creates a copy of the current instance.

  A that is a copy of the current instance.

  The method creates a new instance of the same type as the current instance and then copies the contents of each of the current instance's non-static fields.

  This method is implemented to support the interface.

  The returned object must have the exact same type and invocation list as the current instance.

  The default implementation of the method creates a new instance, which is the exact same type as the current instance, and then copies the contents of each of the current instance's non-static fields. If the field is a value type, a bit-by-bit copy of the field is performed. If the field is a reference type, the object referenced by the field is not copied; instead, the returned object contains a copy of the reference. This behavior is identical to .

  Subclasses of should override to customize the way in which copies of the subclass are constructed.

  0
  1.0.5000.0
  2.0.0.0
  4.0.0.0
- Method
  System.Delegate `System.ParamArray` An array of delegates of the exact same type.

  Concatenates the invocation lists of the specified delegates.

  A new delegate, or if is or has only elements.

  The invocation list of the returned delegate is constructed by concatenating the invocation lists of the delegates in , in increasing subscript order. For example, consider the following situation, in which the elements of delegates have the following invocation lists (where En represents an entry in an invocation list, and null represents an empty invocation list): [0] = E1, [1] = null, [2] = E2 + E3, and [3] = E4 + E5 + E6. When these elements are combined, the resulting delegate contains the invocation list E1 + E2 + E3 + E4 + E5 + E6.

  Null elements in are not included in the returned delegate.

  The invocation list of the returned delegate can contain duplicate methods.

  The non- delegates in are not of the same type.

  0
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  System.Runtime.InteropServices.ComVisible(true)
- Method
  System.Delegate `` `` The delegate whose invocation list will be first in the invocation list of the new delegate. The delegate whose invocation list will be last in the invocation list of the new delegate.

  Concatenates the invocation lists of the specified delegates.

  A delegate, or .

  The following table describes the value returned when or is .

  a
  b Return Value
  null
  null null
  null
  non-null
  non-null
  null

  When and are non-null, this method returns a new delegate with the concatenated invocation lists of and .

  Unless or is , and are required to be the exact same type.

  Consider the following situation, in which D1, D2, D3, D4, and D5 are delegate instances of the same type, D1's invocation list has one entry, E1, and D2's invocation list has one entry, E2.

  Then, D3 = Combine(D1, D2) results in D3's having an invocation list of E1 + E2.

  Then, D4 = Combine(D2, D1) results in D4's having an invocation list of E2 + E1.

  Then, D5 = Combine(D3, D4) results in D5's having an invocation list of E1 + E2 + E2 + E1.

  The invocation list of the returned delegate can contain duplicate methods.

  is useful for creating event handlers that call multiple methods each time an event occurs.

  and are not and not of the same type.

  0
  1.0.5000.0
  2.0.0.0
  4.0.0.0
- Method
  System.Delegate `` To be added. To be added. To be added. To be added. 1.0.5000.0 2.0.0.0 4.0.0.0
- Method
  System.Delegate `` `` The of to return. This is required to derive from . A that reflects a static method.

  Returns a new delegate with the specified static method as its invocation list.

  A of type that invokes

  This method is used to dynamically create delegates that invoke static methods. To create a delegate that invokes instance methods, see (, , ).

  or is .

  does not derive from .

  -or-

  does not reflect a static method.

  The method of the delegate was not found.

  The caller does not have the required permission.

  Requires permission to access type information. See

  The method of the delegate was not found.

  1
  Reflection
  1.0.5000.0
  2.0.0.0
  4.0.0.0
- Method
  System.Delegate `` `` `` To be added. To be added. To be added. To be added. To be added. To be added. 2.0.0.0 4.0.0.0
- Method
  System.Delegate `` `` `` The of the delegate to return. This is required to derive from . An instance of an object that implements . A containing the name of the instance method to be invoke on .

  Returns a new delegate with the specified target and instance method as its invocation list.

  A of type that invokes on .

  This method is used to dynamically create delegates that invoke instance methods. To create a delegate that invokes static methods, see (, , ).

  is .

  does not derive from .

  -or-

  is not an instance method.

  -or-

  does not implement .

  The caller does not have the required permission.

  Requires permission to access type information. See 1 Reflection 1.0.5000.0 2.0.0.0 4.0.0.0
- Method
  System.Delegate `` `` `` To be added. To be added. To be added. To be added. To be added. To be added. 2.0.0.0 4.0.0.0
- Method
  System.Delegate `` `` `` The of delegate to return. This is required to derive from . A representing the class that implements . A containing the name of the static method implemented by .

  Returns a new delegate with the specified static method as its invocation list.

  A of type that invokes

  This method is used to dynamically create delegates that invoke static methods. To create a delegate that invokes instance methods, see (, , ).

  is .

  does not derive from .

  -or-

  is not a static method.

  -or-

  does not implement .

  The caller does not have the required permission.

  Requires permission to access type information. See 1 Reflection 1.0.5000.0 2.0.0.0 4.0.0.0
- Method
  System.Delegate `` `` `` `` To be added. To be added. To be added. To be added. To be added. To be added. To be added. 2.0.0.0 4.0.0.0
- Method
  System.Delegate `` `` `` `` To be added. To be added. To be added. To be added. To be added. To be added. To be added. 1.0.5000.0 2.0.0.0 4.0.0.0
- Method
  2.0.0.0
  4.0.0.0
  System.Delegate `` `` `` `` To be added. To be added. To be added. To be added. To be added. To be added. To be added.
- Method
  System.Delegate `` `` `` `` `` To be added. To be added. To be added. To be added. To be added. To be added. To be added. To be added. 2.0.0.0 4.0.0.0
- Method
  2.0.0.0
  4.0.0.0
  System.Delegate `` `` `` `` `` To be added. To be added. To be added. To be added. To be added. To be added. To be added. To be added.
- Method
  System.Object `System.ParamArray`

  An array of instances that are to be passed to the methods in the invocation list of the current instance. Specify if the methods invoked by the current instance do not take arguments.

  Causes a delegate to invoke the methods in its invocation list using the specified arguments.

  The returned by the last method in the invocation list of the current instance.

  To be added.

  The type of one or more elements in is invalid as a parameter to the methods implemented by the current instance.

  The caller does not have the required permissions.

  -or-

  The number, order or type of parameters listed in is invalid.

  A method in the invocation list of the current instance is an instance method and its target object is .

  -or-

  A method in the invocation list of the current instance was invoked on a target object or a class that does not implement it.

  The number of elements in is not equal to the number of parameters required by the methods invoked by the current instance.

  A method in the invocation list of the current instance threw an exception. 1 Reflection 1.0.5000.0 2.0.0.0 4.0.0.0
- Method
  System.Object `` To be added. To be added. To be added. To be added. 1.0.5000.0 2.0.0.0 4.0.0.0
- Method
  System.Boolean `` The to compare with the current instance.

  Determines whether the specified object is equal to the current instance.

  if is equal to the current instance, otherwise .

  Two delegates are equal if they are not null and are of the exact same type, their invocation lists contain the same number of elements, and every element in the invocation list of the first delegate is equal to the element in the corresponding position in the invocation list of the second delegate.

  Two invocation list elements are equal if they invoke the same instance method on the same target instance, or they invoke the same static method.

  This method overrides .

  0
  1.0.5000.0
  2.0.0.0
  4.0.0.0
- Method
  System.Int32

  Generates a hash code for the current instance.

  A containing the hash code for this instance.

  The algorithm used to generate the hash code is unspecified.

  This method overrides .

  0
  1.0.5000.0
  2.0.0.0
  4.0.0.0
- Method
  System.Delegate[]

  Returns the invocation list of the current delegate.

  An ordered set of instances whose invocation lists collectively match those of the current delegate.

  The array contains a set of delegates, each having an invocation list of one entry. Invoking these delegates sequentially, in the order in which they appear in the array, produces the same results as invoking the current delegate.

  Override when subclassing Delegate.

  0
  1.0.5000.0
  2.0.0.0
  4.0.0.0
- Method
  System.Reflection.MethodInfo To be added. To be added. To be added. 1.0.5000.0 2.0.0.0 4.0.0.0
- Method
  System.Void `` `` To be added. To be added. To be added. To be added. 1.0.5000.0 2.0.0.0 4.0.0.0
- Property
  System.Reflection.MethodInfo

  Gets the last method in a delegate's invocation list.

  A .

  This property is read-only.

  The caller does not have the required permissions. Requires permission to access type information. See . 1 Reflection 1.0.5000.0 2.0.0.0 4.0.0.0
- Method
  System.Boolean `` `` The first delegate to compare. The second delegate to compare.

  Determines whether the specified delegates are equal.

  if .Equals() returns ; otherwise, .

  See .

  0
  1.0.5000.0
  2.0.0.0
  4.0.0.0
- Method
  System.Boolean `` `` The first delegate to compare. The second delegate to compare.

  Determines whether the specified Delegates are not equal.

  if .Equals() returns ; otherwise, .

  See .

  0
  1.0.5000.0
  2.0.0.0
  4.0.0.0
- Method
  System.Delegate `` `` The delegate from which to remove the invocation list of . The delegate that supplies the invocation list to remove from .

  Removes the invocation list of a from the invocation list of another delegate.

  Returns a new delegate, , or .

  If and are not , are not equal, and the invocation list of is contained in the invocation list of source, returns a new delegate with the invocation list of removed from the invocation list of .

  If the invocation lists of and are equal, returns .

  If the invocation list of is not found in the invocation list of , returns .

  The following table describes the value returned when or is .

  Return value
  null
  null null
  null
  non-null null
  non-null
  null

  The invocation list of is required to be an exact match of a contiguous set of elements in the invocation list of . If the invocation list of occurs more than once in the invocation list of , the last occurrence is removed.

  The following example demonstrates the method.

  `using System; class MyClass { public string InstanceMethod(string s) { return ("Instance String " + s); } } class MyClass2 { public string InstanceMethod2(string s) { return ("Instance String2 " + s); } } public delegate string DelegatedMethod(string s); class TestClass { public static void WriteDelegate (string label, Delegate d) { Console.WriteLine("Invocation list targets for {0}:",label); foreach(Delegate x in d.GetInvocationList()) Console.WriteLine("{0}",x.Target); } public static void Main() { MyClass myInstance = new MyClass(); DelegatedMethod delInstance = new DelegatedMethod(myInstance.InstanceMethod); MyClass2 myInstance2 = new MyClass2(); DelegatedMethod delInstance2 = new DelegatedMethod(myInstance2.InstanceMethod2); DelegatedMethod [] sourceArray = {delInstance, delInstance2, delInstance2, delInstance}; DelegatedMethod [] remove1 = {delInstance}; DelegatedMethod [] remove2 = {delInstance2, delInstance2}; DelegatedMethod [] remove3 = {delInstance2, delInstance}; DelegatedMethod [] remove4 = {delInstance, delInstance2}; DelegatedMethod [] remove5 = {delInstance, delInstance}; Delegate source = Delegate.Combine(sourceArray); // Display invocation list of source TestClass.WriteDelegate("source", source); //Test 1: value occurs in source twice. Delegate value1 = Delegate.Combine(remove1); Delegate result1 = Delegate.Remove(source, value1); TestClass.WriteDelegate("value1", value1); if (result1==null) { Console.WriteLine("removal test 1 result is null"); } else { TestClass.WriteDelegate("result1", result1); } //Test 2: value matches the middle two elements of source. Delegate value2 = Delegate.Combine(remove2); Delegate result2 = Delegate.Remove(source, value2); TestClass.WriteDelegate("value2", value2); if (result2==null) { Console.WriteLine("removal test 2 result2 is null"); } else { TestClass.WriteDelegate("result2", result2); } //Test 3: value matches the last two elements of source. Delegate value3 = Delegate.Combine(remove3); Delegate result3 = Delegate.Remove(source, value3); TestClass.WriteDelegate("value3", value3); if (result3==null) { Console.WriteLine("removal test 3 result3 is null"); } else { TestClass.WriteDelegate("result3", result3); } //Test 4: value matches the first two elements of source. Delegate value4 = Delegate.Combine(remove4); Delegate result4 = Delegate.Remove(source, value4); TestClass.WriteDelegate("value4", value4); if (result4==null) { Console.WriteLine("removal test 4 result4 is null"); } else { TestClass.WriteDelegate("result4", result4); } //Test 5: value does not occur in source. Delegate value5 = Delegate.Combine(remove5); Delegate result5 = Delegate.Remove(source, value5); TestClass.WriteDelegate("value5", value5); if (result5==null) { Console.WriteLine("removal test 5 result5 is null"); } else { TestClass.WriteDelegate("result5", result5); } //Test 6: value exactly matches source. Delegate result6 = Delegate.Remove(source, source); TestClass.WriteDelegate("value=source", source); if (result6==null) { Console.WriteLine("removal test 6 result6 is null"); } else { TestClass.WriteDelegate("result6", result6); } } }`

  The output is

  Invocation list targets for source:

  MyClass

  MyClass2

  MyClass2

  MyClass

  Invocation list targets for value1:

  MyClass

  Invocation list targets for result1:

  MyClass

  MyClass2

  MyClass2

  Invocation list targets for value2:

  MyClass2

  MyClass2

  Invocation list targets for result2:

  MyClass

  MyClass

  Invocation list targets for value3:

  MyClass2

  MyClass

  Invocation list targets for result3:

  MyClass

  MyClass2

  Invocation list targets for value4:

  MyClass

  MyClass2

  Invocation list targets for result4:

  MyClass2

  MyClass

  Invocation list targets for value5:

  MyClass

  MyClass

  Invocation list targets for result5:

  MyClass

  MyClass2

  MyClass2

  MyClass

  Invocation list targets for value=source:

  MyClass

  MyClass2

  MyClass2

  MyClass

  removal test 6 result6 is null

  0
  1.0.5000.0
  2.0.0.0
  4.0.0.0
- Method
  System.Delegate `` `` The delegate from which to remove all matching occurrences of the invocation list of . The delegate that supplies the invocation list to remove from .

  Removes all matching occurrences of the invocation list of a from the invocation list of another delegate.

  Returns a new delegate, , or .

  If and are not , are not equal, and the invocation list of is contained in the invocation list of source, returns a new delegate with all matching occurrences of the invocation list of removed from the invocation list of .

  If the invocation lists of and are equal, or if contains only a succession of invocation lists equal to , returns .

  If the invocation list of is not found in the invocation list of , returns .

  The following table describes the value returned when or is .

  Return value
  null
  null null
  null
  non-null null
  non-null
  null

  The invocation list of is required to be an exact match of a contiguous set of elements in the invocation list of. If the invocation list of occurs more than once in the invocation list of , all occurrences are removed.

  0
  1.0.5000.0
  2.0.0.0
  4.0.0.0
- Method
  System.Delegate `` To be added. To be added. To be added. To be added. 1.0.5000.0 2.0.0.0 4.0.0.0
- Property
  System.Object

  Gets the last object upon which a delegate invokes an instance method.

  A instance, or if the delegate invokes only static methods.

  This property is read-only.

  If the delegate invokes only static methods, this property returns . If the delegate invokes one or more instance methods, this property returns the target of the last instance method/target pair in the invocation list.

  Example 1:

  The following example gets the property values for two delegates. The first delegate invokes a static method, and the second invokes an instance method.

  `using System; public delegate string DelegatedMethod(string s); class MyClass { public static string StaticMethod(string s) { return ("Static method Arg=" + s); } public string InstanceMethod(string s) { return ("Instance method Arg=" + s); } } class TestClass { public static void Main() { MyClass myInstance = new MyClass(); //Create delegates from delegate type DelegatedMethod. DelegatedMethod delStatic = new DelegatedMethod(MyClass.StaticMethod); DelegatedMethod delInstance = new DelegatedMethod(myInstance.InstanceMethod); object t = delStatic.Target; Console.WriteLine ("Static target is {0}", t==null ? "null":t); t = delInstance.Target; Console.WriteLine ("Instance target is {0}", t==null ? "null":t); } }`

  The output is

  Static target is null

  Instance target is MyClass

  Example 2:

  The following example gets the property value for three delegates created using instance methods, static methods, and a combination of the two.

  `using System; class MyClass { public static string StaticMethod(string s) { return ("Static String " + s); } public string InstanceMethod(string s) { return ("Instance String " + s); } } class MyClass2 { public static string StaticMethod2(string s) { return ("Static String2 " + s); } public string InstanceMethod2(string s) { return ("Instance String2 " + s); } } public delegate string DelegatedMethod(string s); class TestClass { public static void Main() { DelegatedMethod delStatic = new DelegatedMethod(MyClass.StaticMethod); DelegatedMethod delStatic2 = new DelegatedMethod(MyClass2.StaticMethod2); MyClass myInstance = new MyClass(); DelegatedMethod delInstance = new DelegatedMethod(myInstance.InstanceMethod); MyClass2 myInstance2 = new MyClass2(); DelegatedMethod delInstance2 = new DelegatedMethod(myInstance2.InstanceMethod2); Delegate d = Delegate.Combine(delStatic, delInstance ); Delegate e = Delegate.Combine(delInstance,delInstance2); Delegate f = Delegate.Combine(delStatic, delStatic2 ); if (d!=null) { Console.WriteLine("Combined 1 static, 1 instance, same class:"); Console.WriteLine("target...{0}", d.Target == null ? "null" : d.Target); foreach(Delegate x in d.GetInvocationList()) Console.WriteLine("invoke element target: {0}",x.Target); } Console.WriteLine(""); if (e!=null) { Console.WriteLine("Combined 2 instance methods, different classes:"); Console.WriteLine("target...{0}", e.Target == null ? "null" : e.Target); foreach(Delegate x in e.GetInvocationList()) Console.WriteLine("invoke element target: {0}",x.Target); } Console.WriteLine(""); if (f!=null) { Console.WriteLine("Combined 2 static methods, different classes:"); Console.WriteLine("target...{0}", f.Target == null ? "null" : f.Target); foreach(Delegate x in f.GetInvocationList()) Console.WriteLine("invoke element target: {0}",x.Target); } } }`

  The output is

  Combined 1 static, 1 instance, same class:

  target...MyClass

  invoke element target:

  invoke element target: MyClass

  Combined 2 instance methods, different classes:

  target...MyClass2

  invoke element target: MyClass

  invoke element target: MyClass2

  Combined 2 static methods, different classes:

  target...null

  invoke element target:

  invoke element target:

  0
  1.0.5000.0
  2.0.0.0
  4.0.0.0
0