Information Technology Reference
In-Depth Information
Deleting Methods from a Delegate
You can also delete a method from a delegate, using the
-=
operator.
The following code shows the use of the
-=
operator. Figure 15-7 shows the result of this
code when applied to the delegate illustrated in Figure 15-6.
delVar -= SCl.m3; // Delete the method from the delegate.
Figure 15-7.
Result of subtracting a method from a delegate
As with adding a method to a delegate, the resulting delegate is actually a new delegate.
The new delegate is a copy of the old delegate—but without the reference to the method that
was removed.
The following are some things to remember when removing methods:
If there are multiple entries for a method in the invocation list, the
-=
operator starts
searching at the bottom of the list and removes the first instance it finds of the matching
method.
Attempting to delete a method that is not in the delegate has no effect.
Attempting to invoke an empty delegate throws an exception.
You can check whether a delegate's invocation list is empty by comparing the delegate
to
null
. If the invocation list is empty, the delegate is
null
.