在带有流阅读器的 Using 语句中检查 null 的最佳方法是什么?
What is the best way to check for null in Using statement with streamreader?
我有一个 HttpWebResponse 和 StreamReader resharper 表明我的 httpResponse.GetResponseStream() 可能为空,但我不确定语法是否正确。
using(var httpResponse = (HttpWebResponse)request.GetResponse())
{
if (httpResponse.StatusCode == HttpStatusCode.OK)
{
//This line is where Resharper Is complaining
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
...
}
}
else
{...
只需检查 httpResponse.GetResponseStream()
是否 return 为空值:
using(var httpResponse = (HttpWebResponse)request.GetResponse())
{
if (httpResponse.StatusCode == HttpStatusCode.OK)
{
var responseStream = httpResponse.GetResponseStream();
if (responseStream != null)
{
// Line reached only if httpResponse.GetResponseStream() isn't null
using (var streamReader = new StreamReader(responseStream))
{
var result = streamReader.ReadToEnd();
...
}
}
}
else
{
...
}
}
我有一个 HttpWebResponse 和 StreamReader resharper 表明我的 httpResponse.GetResponseStream() 可能为空,但我不确定语法是否正确。
using(var httpResponse = (HttpWebResponse)request.GetResponse())
{
if (httpResponse.StatusCode == HttpStatusCode.OK)
{
//This line is where Resharper Is complaining
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
...
}
}
else
{...
只需检查 httpResponse.GetResponseStream()
是否 return 为空值:
using(var httpResponse = (HttpWebResponse)request.GetResponse())
{
if (httpResponse.StatusCode == HttpStatusCode.OK)
{
var responseStream = httpResponse.GetResponseStream();
if (responseStream != null)
{
// Line reached only if httpResponse.GetResponseStream() isn't null
using (var streamReader = new StreamReader(responseStream))
{
var result = streamReader.ReadToEnd();
...
}
}
}
else
{
...
}
}