views:

56

answers:

2

When I write something like this:

using (var connection = new SqlConnection("ConnectionString"))
{
    using(var cmd= new SqlCommand("Command"))
    {
        using (var reader = cmd.ExecuteReader())
        {
            while (reader.Read())
            {
            }
        }
    }
}

ReSharper shows warning on reader.Read(), and tells that reader can be null.

But in what cases can it be null? As I know if command returns nothing reader is not null, it only have nothing.

+1  A: 

I think it cannot be null - but resharper does not know that SqlCommand.ExeuteReader() will never return null.
As far as I know there is some xml they use to tell witch functions may or may not return null and it can be set there. I do not know the name and location of the xml though.

Itay
+5  A: 

Given that this is reported on YouTrack as a bug (twice - here and here), it looks like the nullity annotations that are shipped with R# are deficient in this respect.

Have a look in your ReSharper_installation_dir\Bin\ExternalAnnotations folder - this is where the annotations files get installed. For me, with v5, there is in here a file System.Data\System.Data.Nullness.xml which contains this annotation:

<member name="M:System.Data.SqlClient.SqlCommand.ExecuteReader">
  <attribute ctor="M:JetBrains.Annotations.CanBeNullAttribute.#ctor" />
</member>

If by your own inspection you are satisfied that this is wrong, and that SqlCommand.ExecuteReader never returns null, you should change this to

<member name="M:System.Data.SqlClient.SqlCommand.ExecuteReader">
  <attribute ctor="M:JetBrains.Annotations.NotNullAttribute.#ctor" />
</member>
AakashM
Thanks a lot :)
Pavel Belousov