When would SqlCommand.ExecuteReader() return null?

Resharper is correct, it can return null in potential.

It does not matter if a specific implementation for ExecuteReader() will not allow to bubble up a null value - the fact remains that IDataReader is an object which can contain (or point to) null.

  • What if in the future you decide to use a different implementation of IDbCommand?
  • What if the next update of that IDbCommnd implementation will contain a different flow in the code which will allow to bubble up null?

You do not need to know what happens inside the implementation of an interface in order to use it correctly - you just need to know the interface, and right now the interface allows null as a return value.


It's a false positive.

Reflecting on SqlDataReader.ExecuteReader, I can see that the only way the reader is returned as null is if the internal RunExecuteReader method is passed 'false' for returnStream, which it isn't.

In the depths of SqlDataReader, a reader constructor is always called at some point, so I'm pretty sure this is not physically possible for ExecuteReader to return null.