C# 可能的NullreferenceException

C# 可能的NullreferenceException,c#,resharper,nullreferenceexception,C#,Resharper,Nullreferenceexception,Resharper显示“可能的System.NullReferenceException”警告。然而,我不知道如何才能得到一个 public class PlaceController : PlanningControllerBase { [Authorize] public ActionResult StartStop(int id) { if (Request != null && Request.Cookies != null &am

Resharper显示“可能的System.NullReferenceException”警告。然而,我不知道如何才能得到一个

public class PlaceController : PlanningControllerBase
{
    [Authorize]
    public ActionResult StartStop(int id)
    {
        if (Request != null && Request.Cookies != null && Request.Cookies["place"] != null)
        {
            if (Request.Cookies["place"].Value != null)//Possible NullReferenceException?
            {
                string placeInformation = Request.Cookies["place"].Value;//Possible NullReferenceException?
                //...
            }
        }
    }
}
如果我检查所有字段,这怎么能提供空引用?使用以下选项不会显示警告:

Request.Cookies[0];//Index instead of name

编辑:更新代码。

我假设检查程序没有检查传递给CookieCollection索引器的字符串值是否每次都相同。我想如果您将代码重组为:

if (Request != null && Request.Cookies != null) 
{
    var place = Request.Cookies["place"];
    if (place != null && place.Value == null) 
    { 
        string placeInformation = place.Value;
    } 
}

它可能会工作。

错误您不想要
请求。Cookies[“place”]。Value!=空

,现在您只需将placeInformation设置为null。

您不需要听每个警告。
Request
对象和
Cookies
对象将永远不会为空,因此这就是您所需要的

var placeCookie = Request.Cookies["place"]; 
if (placeCookie != null)
{
    string placeInformation = placeCookie.Value;
}

Request.Cookies[“place”]。Value
为空或这是一个错误时,是否有理由要设置变量
placeInformation
,我认为在你的日常生活中使用词汇的确很棒。英语不是我的母语。我想用“是的,…。@Cara-我并不是说你的英语不正确。我只是出于某种奇怪的原因说我确实喜欢这个词。