Delegates Part 4
Delegates Part 4
The Delegate base class overrides Object’s virtual Equals method. The MulticastDelegate type
inherits Delegate’s implementation of Equals. Delegate’s implementation of Equals compares two
delegate objects to see if their _target and _methodPtr fields refer to the same object and method. If
these two fields match, then Equals returns true; otherwise Equals returns false. The following code
demonstrates this:
// Construct 2 delegate objects that refer to the
// same target/method
Feedback fb1 = new Feedback(FeedbackToConsole);
Feedback fb2 = new Feedback(FeedbackToConsole);
In addition, both the Delegate and MulticastDelegate types provide overloads for the equality (==)
and inequality (!=) operators. Therefore, you can use these operators instead of calling the Equals
method. The following code is identical to that shown above:
// Even though fb1 and fb2 refer to two different objects internally,
// they both refer to the same callback target/method.
Console.WriteLine(fb1 == fb2); // Displays "True"