【发布时间】:2015-08-02 13:31:27
【问题描述】:
我对 C# 还很陌生,感觉这可能是一个显而易见的答案,因为我理解错误的含义——但我终生无法看到如何修复它!我的第二个 if 语句收到“检测到无法访问的代码”警告,所以我意识到它没有被调用,我只是不明白我的错误在哪里,或者如何修复它。任何帮助将不胜感激!
我遇到问题的代码的 sn-p 是:
bool valid = true;
if (txtFirst.Text.Length < 1 || txtLast.Text.Length < 1 || txtAddress.Text.Length < 1 || txtCity.Text.Length < 1 || txtState.Text.Length < 1)
{
return false;
}
string usZip = @"^\d{5}$|^\d{5}-\d{4}$";
Regex re = new Regex(usZip);
return re.IsMatch(txtZip.Text);
if (re.IsMatch(txtZip.Text))
return (true);
else
return (false);
return valid;
valid = false;
【问题讨论】:
-
您将无条件返回第二个 if 语句的正上方:
return re.IsMatch(txtZip.Text);。任何低于该回报的东西都不会达到。 -
我已经编辑了你的标题。请参阅“Should questions include “tags” in their titles?”,其中的共识是“不,他们不应该”。
标签: c# unreachable-code