此代码段中是否存在无法访问的代码?我不这么认为,但是Resharper却告诉我了

The*_*att 14 .net c# resharper .net-3.5 visual-studio

我在代码审查中遇到了以下方法.在循环内部,Resharper告诉我这if (narrativefound == false)是不正确的,因为narrativeFound总是如此.我不认为是这种情况,因为为了设置narrativeFound为true,它必须首先传递条件字符串比较,那么它怎么总是真的呢?我错过了什么吗?这是Resharper或我们的代码中的错误吗?

public Chassis GetChassisForElcomp(SPPA.Domain.ChassisData.Chassis asMaintained, SPPA.Domain.ChassisData.Chassis newChassis)
{
    Chassis c = asMaintained;
    List<Narrative> newNarrativeList = new List<Narrative>();

    foreach (Narrative newNarrative in newChassis.Narratives)
    {
         bool narrativefound = false; 

         foreach (Narrative orig in asMaintained.Narratives)
         {
                if (string.Compare(orig.PCode, newNarrative.PCode) ==0 )
                {
                          narrativefound = true;
                          if (newNarrative.NarrativeValue.Trim().Length != 0)
                          {
                             orig.NarrativeValue = newNarrative.NarrativeValue;
                             newNarrativeList.Add(orig);                            
                          }
                          break;
                }
                if (narrativefound == false)
                {
                     newNarrativeList.Add(newNarrative); 
                }
         }
    }

    c.SalesCodes = newChassis.SalesCodes;
    c.Narratives = newNarrativeList;
    return c; 
}
Run Code Online (Sandbox Code Playgroud)

Mar*_*ers 31

narrativefound当控制到达该语句时,变量永远不会为真:

narrativefound = true;
// ...
break;  // This causes control to break out of the loop.
Run Code Online (Sandbox Code Playgroud)

我认为Resharper试图告诉你情况narrativefound == false总是如此.

  • 确切地说 - *条件*将始终为真; `narrativeFound`总是假的. (5认同)