5

What is the closest equivalent to Java's Future<T> in C#?

For example, what would the closest reconstruction of the following be in C#:

public class FutureMethodCall implements Future {
    private Future<APIResponse> methodCall;

    public boolean cancel(boolean mayInterruptIfRunning) {
        return this.methodCall.cancel(mayInterruptIfRunning);
    }

    public APIResponse get() throws ExecutionException, InterruptedException {
        return this.methodCall.get();
    }

    ...
}

Thanks in advance!

JP.
  • 5,536
  • 7
  • 58
  • 100
  • 1
    You can google for Task – Adam Moszczyński Oct 24 '13 at 05:48
  • 1
    If you can't use Task (because you are stuck in an older version of C#) try my promises implementation: https://github.com/Real-Serious-Games/C-Sharp-Promise. Also available on nuget: https://www.nuget.org/packages/RSG.Promise/ – Ashley Davis Jan 30 '15 at 06:17

1 Answers1

4

I'm not sure what a Future does in Java, but from the code it looks like you are executing code at a later time that runs asyncronously and is cancelable.

Have a look at Tasks in C#, they offer the same capabilities.

nvoigt
  • 75,013
  • 26
  • 93
  • 142