Is there any overhead in the use of anonymous methods?

后端 未结 8 1770
遇见更好的自我
遇见更好的自我 2021-02-15 17:27

I would like to know if there is any overhead incurred through the use of anonymous methods when creating a Background worker.

for example:

public void S         


        
8条回答
  •  你的背包
    2021-02-15 17:54

    There is a small difference in how named methods and anonumous methods are handled when you create a delegate from them.

    Delegates for anonymous methods are cached, so there is a small overhead for checking if the delegate already exists in the cache. On the other hand, if you run the method more than once, it will reuse the cached delegate instead of creating a new one.

    Delegates for named methods are not cached, so it will be created each time.

    Other than that there is no difference. The anonumous method will be created at compile time and exists in the code just like a regular method, only with a name that only the compiler knows about.

提交回复
热议问题