在streamreader的Using语句中检查空值的最佳方法是什么?

时间:2019-04-17 19:21:57

标签: c# .net-core streamreader httpwebresponse

我有一个HttpWebResponse,并且StreamReader resharper指示我的httpResponse.GetResponseStream()可能为null,但是我不确定正确的语法。

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
     {...

1 个答案:

答案 0 :(得分:2)

只需检查["cat"]不返回空值即可:

httpResponse.GetResponseStream()