1

In my c# winform, i am using Timer.

  Timer timer = new Timer(); 

For tick event of Timer we can choose from two ways.

1. timer.Tick += new EventHandler(timer_Tick); //event
2. timer.Tick += delegate {/*to perform my operations on tick*/}; //anonymous method

Can any one tell me difference between these two?

also third type can be :

timer.Tick += Timer_Tick;

Will it make any difference?

Arc
  • 87
  • 7
  • 1
    I don't think there is a difference. It just depends on whether the method body and your preference. If you like to put the handler inline, put it inline. Note that you can also use lambdas. – Sweeper Aug 17 '17 at 06:22
  • 1
    [I just put it here](https://stackoverflow.com/q/29155/1849444). – teo van kot Aug 17 '17 at 06:23

3 Answers3

1

the main difference in your example is the structure of the method that will be performed. In the first case:

  1. timer.Tick += new EventHandler(timer_Tick); //event

the method timer_Tick looks like this:

public void timer_Tick(object sender, EventArgs e)
{

}

so you can use the parameters object sender and EventArgs e. What you actually do here is to call the constructor of EventHandler and you pass a delegate/pointer to a method into it.

Although this structure is given by the Tick property in your second case:

  1. timer.Tick += delegate {/to perform my operations on tick/}; //anonymous method

you create an anonymous method and actually hide the parameter with the delegate keyword and you have no access to them. Also you will not be able to call this method from somewhere else. You cannot simulate the firing of the event and you cannot reuse the code.

Here you could use a lambda expression to actually specify the parameters so that you can have access to them:

timer.Tick += (s, e) => { };

The third case:

timer.Tick += Timer_Tick;

is a short version of the first case. Here Visual Studio will infer the correct structure of the method for you without specifying the type of the event. If the method Timer_Tick that you implement will not match the the structure demanded by Tick the compiler will complain!

Example:

enter image description here

The delegate Tick is of type EventHandler . This typespecifies the structure that I wrote in the first case above, you will see it in the documentation. If (as you can see in the picture) the structures of the delegate Tick and the structure of the Timer_Tick method do not match you have an error. Note that in this expression:

timer.Tick += Timer_Tick;

Timer_Tick is just a delegate/a pointer to the method in question which has to be implemented!

For fundamental differences between delegates and events please have a look at this article and this acticle

Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
0

Well one difference would be re-usability. That is if you choose to have a handler method timer_Tick then you can re-use the same handler method for handling other similar sort of events which you can't if you are using a anonymous method or lambda expression.

Rahul
  • 76,197
  • 13
  • 71
  • 125
0

it's not possible to unsubscribe (remove) an anonymous method. because of that it's very bad to use anonymous methods for events. this may result in memory leaks!

WhileTrueSleep
  • 1,524
  • 1
  • 19
  • 32
  • @ WhileTrueSleep, U r right, if it causes memory leak then it can be serious issue for the application. – Arc Aug 17 '17 at 07:17