There is no such thing "subscribing my method to delegate".
You create a delegate instance and add an anonymous method to its invocation list.
System.Action<int, string> myAction = null;
myAction += (number, format) => {
System.Console.WriteLine(format, number);
};
Here
System.Action<int, string>
is a delegate type,
myAction
is a delegate instance.
You should understand, that adding one more method to the invocation list change referential identity of the variable.
System.Action<int, string> myAction = null;
myAction += (number, format) => {
System.Console.WriteLine(format, number);
};
System.Action<int, string> oldAction == myAction;
myAction += (number, format) => { };
So, when you do this operation (
+=
), a brand new delegate instance is created and assigned to a variable. In other words: delegate instances are not mutable. Why? For the same reason as strings: thread safety.
Any questions?
—SA