C# 如何修复错误:“检测到无法访问的代码”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12527060/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I fix the error : "Unreachable Code Detected"
提问by Tim van Laere
public partial class KalenderLeeftijd : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void calBirthDate_SelectionChanged(object sender, EventArgs e)
{
}
private string GetAnswer()
{
DateTime birthday = calBirthDate.SelectedDate;
TimeSpan difference = DateTime.Now.Date - birthday;
int leapYears = CountLeapYears(birthday);
int days = (int)difference.TotalDays - leapYears;
int hours = (int)difference.TotalHours - leapYears * 24;
int years = days / 365;
String answer = String.Format("Age: {0} years", years);
answer += Environment.NewLine;
answer += String.Format("Days: {0}*365+{1} = {2}", years, days - years * 365, days);
answer += Environment.NewLine;
answer += String.Format("Days Hours: {0}*24 = {1}", hours / 24, hours);
return answer;
}
private int CountLeapYears(DateTime startDate)
{
int count = 0;
for (int year = startDate.Year; year <= DateTime.Now.Year; year++)
{
if (DateTime.IsLeapYear(year))
{
DateTime february29 = new DateTime(year, 2, 29);
if (february29 >= startDate && february29 <= DateTime.Now.Date)
{
count++;
}
}
}
return count;
String answer = GetAnswer();
Response.Write(lblAntwoord);
}
}
Why do I get the error : "Unreachable code detected"? - The error is shown on the following line -
String answer = GetAnswer();
为什么会出现错误:“检测到无法访问的代码”?- 错误显示在下一行 -
String answer = GetAnswer();
回答by Rapha?l Althaus
It's just because your code comes after the return statement.
这只是因为您的代码出现在 return 语句之后。
The return statement terminates execution of the methodin which it appears and returns control to the calling method. It can also return an optional value. If the method is a void type, the return statement can be omitted.
If the return statement is inside a try block, the finally block, if one exists, will be executed before control returns to the calling method.
return 语句终止它出现的方法的执行,并将控制权返回给调用方法。它还可以返回一个可选值。如果方法是 void 类型,则可以省略 return 语句。
如果 return 语句在 try 块内,则 finally 块(如果存在)将在控制返回到调用方法之前执行。
http://msdn.microsoft.com/en-us/library/1h3swy84%28v=vs.100%29.aspx
http://msdn.microsoft.com/en-us/library/1h3swy84%28v=vs.100%29.aspx
solution (obvious) :
解决方案(显而易见):
move the unreachable code before the return statement.
将无法访问的代码移到 return 语句之前。
回答by Larry
The statement:
该声明:
return count;
Exits the function. Therefore,
退出函数。所以,
answer = GetAnswer();
Response.Write(lblAntwoord);
cannot be reached.
无法连接。
回答by Adam Houldsworth
Unreachable code is a compiler warning, not error. You have three options:
无法访问的代码是编译器警告,而不是错误。您有三个选择:
- Remove the unreachable code.
- Stop treating warnings as errors in the project properties.
- Move the return statement to below what is currently unreachable.
- 删除无法访问的代码。
- 停止将警告视为项目属性中的错误。
- 将 return 语句移到当前无法访问的下方。
It is unreachable because the flow of the method exits at the returnstatement, and thus will never execute the code below. The compiler can determine this and so can report it. Like I said, these are actually compiler warningsand won't stop a successful build unless you have configured the project to treat warnings as errors.
它是不可达的,因为方法的流程在return语句处退出,因此永远不会执行下面的代码。编译器可以确定这一点,因此可以报告它。就像我说的,这些实际上是编译器警告,除非您已将项目配置为将警告视为错误,否则不会阻止成功构建。
回答by Venkat
The returnstatement terminates the execution of a function and returns control to the calling function. Execution resumes in the calling function at the point immediately following the call
If no returnstatement appears in a function definition, control automatically returns to the calling function after the last statement of the called function is executed
在返回语句终止函数的执行,并将控制返回给调用函数。执行在调用函数中紧跟在调用之后的点处恢复
如果函数定义中没有出现return语句,则在执行完被调用函数的最后一条语句后,控制权自动返回到调用函数
In Your Code :
在您的代码中:
private int CountLeapYears(DateTime startDate)
{
int count = 0;
for (int year = startDate.Year; year <= DateTime.Now.Year; year++)
{
if (DateTime.IsLeapYear(year))
{
DateTime february29 = new DateTime(year, 2, 29);
if (february29 >= startDate && february29 <= DateTime.Now.Date)
{
count++;
}
}
}
return count;//The Execution will be terminated here,the next lines will become unreachable
**String** answer = GetAnswer();
Response.Write(lblAntwoord);
}
}
MSDN LINK :
MSDN 链接:

