If a Tick-handling function will only be used in one context (i.e. always in the same function in combination with the same Timer object), why bother make it a separate function? That's the thought that went through my head when I thought of this.
Is it possible to tie an anonymous function to a Timer's tick event? Here's what I'm trying to do.
Timer myTimer = new Timer();
myTimer.Tick += new EventHandler(function(object sender, EventArgs e) {
MessageBox.Show("Hello world!");
});
A complete example would be:
You use the
delegate
keyword for anonymous methods:In C# 3.0 and later, you can also use lambdas:
You're looking for Anonymous Methods:
You can also omit the parameters:
In C# 3.0, you can also use a Lambda Expression: