tags:

views:

200

answers:

5

I am just curious to know that why the return type of an asynchronous call in c# is IAsynceResult ?

+2  A: 

What else would it be? It can't be the "final" result of the call, as that won't be known yet. Basically it's a value representing the asynchronous call, so that you can later determine whether it's completed, what the result was etc.

It's a shame it's not generic (in the result type) for non-void async calls, but that's due to the legacy of .NET 1 not supporting generics.

EDIT: I nearly mentioned Task<T> originally. The trouble is, there are a lot of places where the method is already declared to return IAsyncResult, and you can't just go changing APIs left, right and centre. I do occasionally wonder how much cleaner .NET would be if MS had waited for generics before releasing. Of course that sort of argument is always applicable, but for generics it's particularly important IMO.

Jon Skeet
I agree - generics clearly provide a much better solution and it's a shame that this area of the .Net framework has been neglected so much since it's inception. That said - it's probably quite an intimidating change to make across the whole framework, so I don't blame them for hesitating!
Andras Zoltan
Of course PFX does provide such a generic return: `Task<T>.Factory.FromAsync` returns a `Task<T>`, and `Task<T>` implements `IAsyncResult`. Finally getting a better model.
Richard
+1  A: 

The IAsyncResult provides the ability to monitor the progress of an asynchronous call, as well as to provide a 'token' that can later be passed to the 'Complete___' method of the async pair of methods that you typically see.

Since the asynchronous call cannot return the actual value of the operation, this interface provides a container for the state of that call that later enables you to get the result.

This is especially important when considering that you might have multiple asynchronous calls to the same method outstanding - the IAsyncResult instance helps identify each of them individually.

Andras Zoltan
+1  A: 

Because it's an asynchronous call, the actual end result is not yet known. So, the return type is essentially your reference to the call you have made so you can keep track of when it completes.

Think of it a bit like a ticket you get when you drop a parcel off to be delivered via recorded/tracked delivery - that ticket gives you a means to check the progress of the delivery and follow it up later.

AdaTheDev
A: 

Because of classic async pattern implementation.

IAsyncResult object returns from the Begin method and represents a single asynchronous operation. It contains methods and properties providing access to some basic information about the asynchronous operation.

Sergey Teplyakov
A: 

Making an asynchronous call is like asking someone to go and do something at their convenience. The IAsyncResult is a handle to that request. You can use it to wait for the task to complete, to query the progress, and get any result and/or exception from the task. What would prefer instead?

Brian Rasmussen