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
}
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;
}
How to fix the issue not all the code paths return value?
I'm not sure why tslint is complaining, but you can write the whole thing way more elegant as:
return str.split(",").every(el => List.includes(el));
or ES6:
return str.split(",").every(el => List.indexOf(el) > -1);
C# error: not all code paths return a value
Your method SearchFiles
only returns a value if isIdentical
is false
. If it's true
, the method never returns.
To remove this error, write something like this:
public static Boolean SearchFiles(string path1, string path2)
{
// do some work to assign a value to isIdentical
// note that it would be idiomatic to just write "return isIdentical;" in this case
// I keep it explicitly here for demonstration purposes
if (isIdentical == false)
{
return false;
}
else
{
return true;
}
}
To your second question: If you declare your method as public static void
you must not return
any value. void
means that the method will not give you anything back.
You might want to have a look at this: Methods (C# Programming Guide), especially the part about return values.
Edit: Since you have your if / else
in a foreach
loop, you need something like this:
public static Boolean SearchFiles(string path1, string path2)
{
foreach(var item in collection)
{
// do some work to assign a value to isIdentical
if (isIdentical == false)
{
return false;
}
else
{
return true;
}
}
// in case the collection is empty, you need to return something
return false;
}
Task return type gives not all code paths return a value
Needs a return
:
return Task.Factory.StartNew(() => { Console.WriteLine("Hello Task library!"); });
Or better:
return Task.Run(() => { Console.WriteLine("Hello Task library!"); });
Error message: not all code paths return a value
You don't have to call return on each conditional statement. Simply track the state with a variable (purchaseState). It is unclear what should happen when a InAppBillingPurchaseException is thrown. I would at least log this error.
public async Task<bool> TestMakePurchase(string productId, string payload)
{
var purchaseSuccesful = false;
var billing = CrossInAppBilling.Current;
try
{
if (await billing.ConnectAsync())
{
var purchase = await billing.PurchaseAsync(productId, ItemType.InAppPurchase, payload);
purchaseSuccesful = purchase != null;
}
}
catch (InAppBillingPurchaseException purchaseEx)
{
//log this error?
}
finally
{
await billing.DisconnectAsync();
}
return purchaseSuccesful;
}
Not all code paths return a value - C#
your methods return type is List<Employee>
. It expects you to return a List<Employee>
object. But you are not returning anything from your method of type List<Employee>
.
following will fix your issue.
public List<Employee> GetEmployees()
{
List<Employee> employees = new List<Employee>();
Employee emp = new Employee();
emp.FirstName = "johnson";
emp.LastName = " fernandes";
emp.Salary = 14000;
employees.Add(emp);
return employees;
}
Related Topics
The Application Called an Interface That Was Marshalled for a Different Thread - Windows Store App
Transparent Background on Winforms
How to Remove Item from Querystring in ASP.NET Using C#
Why Is There Not a 'Fieldof' or 'Methodof' Operator in C#
Multi Processes Read&Write One File
Are Protected Members/Fields Really That Bad
Visual Studio - Resx File Default 'Internal' to 'Public'
Connect to Active Directory via Ldap
Convert String to Int Array Using Linq
Wait for an Ajax Call to Complete with Selenium 2 Webdriver
Moq: Unit Testing a Method Relying on Httpcontext
Sqlite Keeps the Database Locked Even After the Connection Is Closed
Complex Models and Partial Views - Model Binding Issue in ASP.NET MVC 3
How to Set Web.Config File to Show Full Error Message
What's a Good Way of Doing String Templating in .Net
How to Create an Expression Tree Calling Ienumerable<Tsource>.Any(...)