As Darin says, an anonymous function is the way here. You are only trying to specify one bit of behavior, so just accept the accept Action method, and then the caller can use an anonymous function (either an anonymous method or a lambda expression). The calling code will look like this:
changeDelayTimer.Schedule(() => { // Code to run }, TimeSpan.FromSeconds(10));
Or, if you have a lot of code, you want to use a method with group method conversion:
changeDelayTimer.Schedule(MethodContainingCode, TimeSpan.FromSeconds(10));
In Java, I rarely find that I want to do more than redefine one method inside an anonymous inner class, so anonymous functions in C # almost always work just as well.
When porting code from Java to C #, itโs important not to try to preserve the Java idioms - to keep the same goals, but to port to the .NET idiom, which in this case uses the delegate to represent one piece of behavior in an abstract way.
Jon skeet
source share