包含foreach语句的方法只有一个返回

Mic*_*ski 2 c# foreach for-loop return

我有以下为单元测试准备的方法,我知道它将始终运行for-each循环,有没有办法摆脱第二个return语句?

public Enums.GYRStatus GetStatusForTransformer(
            string factoryCode, 
            Enums.Technology technology, 
            string transformerType,
            int transformerSize,
            string transformerModel)
{
   fakeStandardsAndSizesFictionary = new Dictionary<Tuple<string,
                                                    Enums.Technology,
                                                    string, int, string>, int>() 
   {
       { Tuple.Create("SELUD", Technology.CVT,"---", 0, ""), 1} };
   }

   foreach (var pair in fakeStandardsAndSizesFictionary)
   {
       if (pair.Key.Item1 == factoryCode &&
          pair.Key.Item2 == technology &&
          pair.Key.Item3 == transformerType &&
          pair.Key.Item4 == transformerSize &&
          pair.Key.Item5 == transformerModel)
           return (Enums.GYRStatus)pair.Value;
   }
   return (Enums.GYRStatus)1; // second return never used
}
Run Code Online (Sandbox Code Playgroud)

Ale*_*exD 7

你可以替换

return (Enums.GYRStatus)1;
Run Code Online (Sandbox Code Playgroud)

同

throw new InvalidOperationException();
Run Code Online (Sandbox Code Playgroud)

假设永远不会到达这个地方,它在语义上看起来也更正确.