我为单元测试准备了以下方法,我知道它总是会运行 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
}
最佳答案
可以替换
return (Enums.GYRStatus)1;
与
throw new InvalidOperationException();
它在语义上看起来也更正确,假设永远不会到达这个地方。
关于c# - 包含 foreach 语句的方法只有一个返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31720606/