Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a performance difference between function call and an event in C#?

I'll try to clarify my question :

I have a function called Draw (which someone (XNA) calls her 60 times per second), and i have many objects to draw, so i have the following code : void Draw() { obj1.draw(); obj2.draw(); obj3.draw(); .... }

Is there will be a performance impact if instead i will create an event, which will be raised by Draw(), and all the objects would sign up for the event ?

In case i wansn't clear what i'm asking is : Does a call to a function by signing up to an event is different from a regular call ?

like image 356
OopsUser Avatar asked Dec 13 '12 21:12

OopsUser


1 Answers

Regarding performance, I think Jon Skeet's example is pretty conclusive that delegates don't add any significant overhead to performance, and may even improve it.

One factor you do need to consider with events/delegates is handling unhooking objects listening to an event or your reference counts will not reset properly and you would find yourself with memory leaks. Avoid anonymous methods unless you're prepared to store references to them so they can be unwired on Dispose etc.

like image 57
Steve Py Avatar answered Oct 19 '22 23:10

Steve Py