In .NET, when you have a delegate that only has an empty method subscribed to it, does the .NET VM realize this and skip even calling it?
I’m asking because I noticed that the invocation count is set to 2 when you subscribe once but when you unsubscribe it, the value goes down to 0 (not 1).
No, what you are seeing is the MulticastDelegate class optimizing the general case in which there’s only one target method. With just one, it can store the target in the base class’ (Delegate) _methodBase and _target fields.
When there’s more than one then it has to create a list to store the targets. Now it uses its _invocationList field so it can store as many targets as needed. In other words, _invocationCount will never be 1.
You can see this by expanding “base” in the debugger.