Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

I have the following code:

private string GetAnswer()
{
   ....
}

private int CountLeapYears(DateTime startDate)
{
    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();

like image 372
Tim van Laere Avatar asked Nov 28 '22 01:11

Tim van Laere


1 Answers

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.

like image 90
Raphaël Althaus Avatar answered Dec 14 '22 23:12

Raphaël Althaus