Does using delegates slow down my .NET programs?

前端 未结 4 2052
小鲜肉
小鲜肉 2020-12-06 10:20

Does using delegates slow down my programs?

I\'ve been avoiding them because I really have no clue if they make my programs any slower. I know if I cause a (catch

4条回答
  •  失恋的感觉
    2020-12-06 11:03

    Delegates are very, very fast. Not quite as fast as direct method calls, but not far off. The chances of them becoming a bottleneck are miniscule.

    (Likewise exceptions, when used properly, rarely actually cause a performance issue.)

    Would using delegates make your code simpler, more readable, and more robust? If so, use them. Measure your performance carefully, and keep an eye on it. Move away from readability for the sake of performance only when the data is clear.

    I'm sure there are some graphs around showing the speed of delegates vs interfaces vs non-virtual method calls etc - I don't know where they are, but you could always run tests yourself if you're really worried.

提交回复
热议问题