Removes the last occurrence of the invocation list of a delegate from the invocation list of another delegate.
A new delegate with an invocation list formed by taking the invocation list of source and removing the last occurrence of the invocation list of value, if the invocation list of value is found within the invocation list of source. Returns source if value is null or if the invocation list of value is not found within the invocation list of source. Returns a null reference if the invocation list of value is equal to the invocation list of source or if source is a null reference.
If the invocation list of value matches a contiguous set of elements in the invocation list of source, then the invocation list of value is said to occur within the invocation list of source. If the invocation list of value occurs more than once in the invocation list of source, the last occurrence is removed.
The following example demonstrates the Delegate.Remove(Delegate, Delegate) method.
C# Example
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: