Write a C # implementation of the abstract inline class?

I found a great example of code written in java to use the timer classes / timertask ...

I am currently converting this to C # for implementing "Mono for Android", but have problems converting the built-in implementation of the abstract class TimerTask

// Here the original java code private void resetMapChangeTimer() { mChangeDelayTimer.schedule( new TimerTask() { @Override public void run() { // Some code to run } }, longTenSeconds); } 

When I implement this in C #, I would like to create an inherited class from TimerTask (an abstract class), as well as a run method, etc., without creating a separate class (extending TimerTask), as java code does.

Can anyone advise me on C # syntax to create this abstract inline class instead of creating a separate class just for inheritance and implementation of TimerTask?

+7
source share
3 answers

C # does not support inline classes, as in Java. You can define anonymous methods, but not whole classes. Thus, you will need to define a separate class that implements the abstract class.

+6
source

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.

+5
source

If you want to use / extend existing Java classes, you can implement a common helper class:

 class MyTimerTask: TimerTask { private Action action; public MyTimerTask(Action action) { this.action = action; } public override void Run() { if(action != null) action(); } } 

Then you can use anonymous methods:

 mChangeDelayTimer.Schedule(new MyTimerTask(() => { //Do something }), longTenSeconds); 

To have the best C # experience in your project, you can add an extension method. Create an open static class somewhere in your project:

 public static class TimerHelper { public static void Schedule(this DelayTimer /*or whatever your timer class is*/ timer, Action action, int delay) { if(timer == null) throw new ArgumentNullException("timer"); //null check needed because this method is not really an instance method timer.Schedule(new MyTimerTask(action), delay); } } 

Then you can use the following code: (The TimerHelper class must be โ€œvisibleโ€ in the execution context for the extension method to display, so it is publicly available)

 mChangeDelayTimer.Schedule(() => { //Do something }, longTenSeconds); 
0
source

All Articles