Why doesn't BeginInvoke return a link of type AsyncResult?

When you invoke BeginInvoke delegate objects, the system creates an object of type AsyncResult, but BeginInvoke returns an IAsyncResult interface type reference (which indicates an AsyncResult instance). Why does BeginInvoke instead return a reference of type AsyncResult?

thanks

+7
c # delegates
source share
2 answers

This is standard interface-based programming .

By returning IAsyncResult, the structure is free to change the internal implementation later without breaking code written against the APIs. This, in essence, tells us, as developers, that it doesn't matter to us what type of implementation is used if the interface (IAsyncResult) is encountered.

If instead a valid AsyncResult class was returned, then switching to another class will break the existing code.

In addition, this allows the infrastructure to use the same interface (IAsyncResult) in several places, regardless of whether the AsyncResult class is suitable. I took advantage of this personally, as I created my own asynchronous functions that return IAsyncResult under another class that stores information that is important to me. This allows my own code to work as a structure without restricting me to implementing the framework.

+8
source share

Because it does not always return AsyncResult. The delegation task may exist in a different execution context, the RealProxy derived class may implement a proxy server for it. An example is Remoting. Returning an interface type makes this transparent.

+2
source share

All Articles