In the following code, if it cmdwas initialized, I will make it so that I close any open connections before throwing an exception. However, even after I checked that cmdit is not null, I still get a possible warning of null reference in the following lines of code.
Dim cmd As SqlCommand
Try
'Do some Stuff
Catch ex As Exception
If cmd IsNot Nothing AndAlso
cmd.Connection IsNot Nothing AndAlso '<- null cmd warning
cmd.Connection.State = ConnectionState.Open Then '<- null cmd warning
cmd.Connection.Close() '<- null cmd warning
End If
Throw
End Try
I get the following two warnings (possibly one from Resharper and one from Visual Studio):
- The variable "x" cannot be initialized before access. An exceptional link exception may occur at run time.
- BC42104: the variable 'x' is used before it is assigned a value. A null reference exception may occur at runtime.
Visual Studio:
, , - .
, , .
:

, , Null Reference Exception, NullableType, , .
:
: ?
Nothing, cmd IsNot Nothing False, AndAlso .
Dim cmd1 As SqlCommand
Console.Write(cmd1 IsNot Nothing) 'False
Dim cmd2 As SqlCommand = Nothing
Console.Write(cmd2 IsNot Nothing) 'False
, .