According to Jon Skeet's answer anonymous functions are not guaranteed to return equal delegates:
The C# specification explicitly states (IIRC) that if you have two anonymous functions (anonymous methods or lambda expressions) it may or may not create equal delegates from that code. (Two delegates are equal if they have equal targets and refer to the same methods.)
example of problematic code given
button.Click += (s, e) => MessageBox.Show("Woho");
button.Click -= (s, e) => MessageBox.Show("Woho");
Is this the same case for for extension methods as well? For sake of sample simplicity, lets ignore whether it is good idea to extend string
:
var text = "Woho";
// extension method body is MessageBox.Show(text)
button.Click += text.ShowMessageBoxExtension;
button.Click -= text.ShowMessageBoxExtension; // can you safely unsubscribe using delegate2?
var delegate1 = text.ShowMessageBoxExtension;
var delegate2 = text.ShowMessageBoxExtension;
Debug.Assert(delegate1 == delegate2); // or more precisely is this always true?