2

Consider the following code:

public class AsyncRequestHandler : IHttpAsyncHandler
{
   public void EndProcessRequest(IAsyncResult result)
   {
     if (result == null)
     {
       ...
     }
     ...
   }
}

ReSharper suggests that result could never be null (by indicating that the condition is always false). However, since IAsyncResult is a reference type, null is a possible value. I came up with two possibilities:

  • Some kind of metadata available that allows ReSharper to conclude that result could never be null. Looking at the IHttpAsyncHandler source code using ReSharper, I don't see any attributes associated with EndProcessRequest.
  • ReSharper is aware that the IHttpAsyncHandler guarantees that IAsyncResult will never be null. However, the documentation doesn't gives any clues either way.

It seems reasonable that IAsyncResult won't be null since it allows us to track the progress of the asynchronous task. Can someone please provide some insight?

Pooven
  • 1,744
  • 1
  • 25
  • 44

1 Answers1

2

IHttpAsyncHandler is an example of the old-style Asynchronous Programming Model.

Perhaps ReSharper is designed to recognise the pattern.

Note:

The effect of calling the EndOperationName method multiple times with the same IAsyncResult is not defined. Likewise, calling the EndOperationName method with an IAsyncResult that was not returned by the related Begin method is also not defined.

So, you don't have to write code to deal with people not properly calling your code, following this pattern.

Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448