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

时间:2010-05-18 16:47:57

标签: c# .net visual-studio .net-3.5 resharper

我在代码审查中遇到了以下方法。在循环内部,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; 
}

5 个答案:

答案 0 :(得分:31)

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

narrativefound = true;
// ...
break;  // This causes control to break out of the loop.

我认为Resharper试图告诉你条件narrativefound == false将永远是真的。

答案 1 :(得分:4)

您根本不需要narrativeFound变量。在您将其设置为true的范围内,您将从foreach循环中断。如果您未将其设置为true,则不会中断,并将newNarrative添加到newNarrativeList

所以,这可以改写为

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

            newNarrativeList.Add(newNarrative);                 
     }
}

答案 2 :(得分:4)

这是您代码中的错误。

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);                             
                      } 
// narrativeFound == true, but now we exit the for loop
                      break; 
            } 
// narrativeFound is always false here.  The test is redundant
            if (narrativefound == false) 
            { 
                 newNarrativeList.Add(newNarrative);  
            } 
     } 
} 

答案 3 :(得分:1)

R#是正确的,因为如果你将narrativefound变为true,那么你在设置之后就会突然脱离foreach。

答案 4 :(得分:0)

我相信它告诉你,如果将narriativefound设置为true,则退出for循环(break;)。因此,如果if(narriativefound == false)被评估,它将始终具有false值。