C# compiler error: not all code paths return a value
You're missing a return
statement.
When the compiler looks at your code, it's sees a third path (the else
you didn't code for) that could occur but doesn't return a value. Hence not all code paths return a value
.
For my suggested fix, I put a return
after your loop ends. The other obvious spot - adding an else
that had a return
value to the if-else-if
- would break the for
loop.
public static bool isTwenty(int num)
{
for(int j = 1; j <= 20; j++)
{
if(num % j != 0)
{
return false;
}
else if(num % j == 0 && num == 20)
{
return true;
}
}
return false; //This is your missing statement
}
Not all code paths return a value although i do have returns?
Either change every break
in your switch so that it is a return
(if you will vary what you return based on whether the true or the false case is applied) or put a return statement at the end of your method, after your switch is finished with
The compiler isn't kidding; it really can see paths through your code that don't end in a return. You can trace them too.. follow your true
case.. it can reach break, exit the switch and then have no return statement before it hits the end of the method. You might be certain that pairs
will definitely always have a value but the compiler doesn't have that same level of knowledge about the program as you and it doesn't trace back to try and find evidence that pairs will always have a value.. it just sees "return statement inside a loop which is a block that might not execute, means code might not hit that return statement"
In case you're under an illusion as to what default
is for, in a switch
.. it is used when no other case
applies. It is not "something that runs every time, regardless of whether some other case applied or not" so it can't be used as a catch all to return something if other cases didn't
If you're certain that one of your returns in the switch will be hit you can return something useless at the end of the method or better, throw an exception with a meaningful message so that in 6 months when all these "None of the switch statements returned a value in JsonToObjects, the input values were..." start appearing in the logs you have a good pointer to what is going wrong and where. Do not make your error message "this should never be hit"
Not all code paths return a value in C# console application Error Coode:CS0161
If list
is empty (i.e. has no items) or is null, then the loop will not run. This means the code cannot reach any of the return
statements, because they are all within the loop. In that case, there is no way for the program to know what value to return from the method. There is a "path" through the method which does not return a value.
This is an impossible situation - a non-void method must return something, so the compiler will not allow you to build and run the program until you have resolved it by adding an extra return
statement at the end after the loop finishes, to cover the situation I've described.
CS0161: not all code paths return a value
The compiler can't know that on of the if
s will eventually evaluate to true
. You need some default return value after the for
loop, even if it's never reached in practice:
static int LowestDivisor(int x)
{
if (x < 0)
x *= -1;
if (x == 0 || x == 1)
return -1;
for (int i = 2; i <= x; i++)
{
if (x % i == 0)
{
return i;
}
}
// Should never happen...
return x;
}
C# cannot return value 'not all code paths return a value' error
If your isOpponent
variable has a value other than 1
no double is returned.
You have to add another return
statement at the end of your function for the other cases.
The error message 'not all code paths return a value' means, that you don't have a return statement specified for all possible "paths" the program can take.
double calculateSpeed(double isOpponent)
{
// Path 1 when `isOpponent == 1`
if (isOpponent == 1)
{
double speedY = playerCard.Location.Y / stepY;
return speedY;
}
// Path 2 when `isOpponent != 1`
// Here is where you are missing a return statement.
}
calculateSpeed(1)
Main: not all code paths return a value
Return something at the end of Main
. Like this:
public static int Main()
{
List<DocPart> Parts = new List<DocPart>();
var doc = new DocConfig();
doc.Description = "bla bla";
doc.Parts = new List<DocPart>();
doc.Parts.Add(new DocPart { Title = "aaa", TexLine = @"\include{aaa.tex}" });
doc.Parts.Add(new DocPart { Title = "bbb", TexLine = @"\include{bbb.tex}" });
foreach (DocPart part in doc.Parts)
{
Console.WriteLine(part.Title);
{
Console.ReadLine();
return 0;
}
}
return -1;
}
Related Topics
Reach Control from Another Page. Asp.Net
Difference Between a Field and a Property
C# Interfaces. Implicit Implementation Versus Explicit Implementation
Typenamehandling Caution in Newtonsoft Json
Json.Net: How to Deserialize Without Using the Default Constructor
When Correctly Use Task.Run and When Just Async-Await
How to Get the Index of the Current Iteration of a Foreach Loop
How to Configure an App to Run Correctly on a Machine With a High Dpi Setting (E.G. 150%)
What Is the Purpose of "Return Await" in C#
Pass an Instantiated System.Type as a Type Parameter For a Generic Class
How to Read and Parse an Xml File in C#