How can I fix the error : "Unreachable Code Detected"

It's just because your code comes after the return statement.

The return statement terminates execution of the method in 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.

http://msdn.microsoft.com/en-us/library/1h3swy84%28v=vs.100%29.aspx

solution (obvious) :

move the unreachable code before the return statement.


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.

It is unreachable because the flow of the method exits at the return statement, 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 warnings and won't stop a successful build unless you have configured the project to treat warnings as errors.


The statement:

return count;

Exits the function. Therefore,

answer = GetAnswer(); 
Response.Write(lblAntwoord);  

cannot be reached.