Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I'm not so inclined to think this is a "bug". It appears moreover that you're assuming some behaviour in the CLR that simply does not exist.</p> <p>The important thing to understand here is that you are returning a new anonymous method (and initialising a new closure class) each time you call the <code>CreateDelegate</code> method. It seems that you are experting the <code>delegate</code> keyword to use some sort of pool for anonymous methods internally. The CLR certainly does not do this. A delegate to the anonymous method (as with a lambda expression) is created in memory each time you call the method, and since the equality operator does of course compare <em>references</em> in this situation, it is the expected result to return <code>false</code>.</p> <p>Although your suggested behaviour may have some benefits in certain contexts, it would probably be quite complicated to implement, and would more likely lead to unpredictable scenarios. I think the current behaviour of generating a new anonymous method and delegate on each call is the right one, and I suspect this is the feedback you will get on Microsoft Connect as well.</p> <p>If you are quite insistent on having the behaviour you described in your question, there is always the option of <a href="http://en.wikipedia.org/wiki/Memoization" rel="noreferrer">memoizing</a> your <code>CreateDelegate</code> function, which would insure that the same delegate is returned each time for the same parameters. Indeed, because this is so easy to implement, it is probably one of the several reasons why Microsoft did not consider implementing it in the CLR.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload