I have a method implemented from an interface which looks as follows..
public Task CreateAsync(ApplicationUser user)
{
if (user == null)
{
throw new ArgumentNullException("user");
}
Task.Factory.StartNew(() => { Console.WriteLine("Hello Task library!"); });
//I even tried
//Task.Run(() => { Console.WriteLine("Hello Task library!"); });
}
The above code gives me an error not all code paths return a value.
The error "Not all code paths return a value" occurs when some of the code paths in a function don't return a value. To solve the error, make sure to return a value from all code paths in the function or set noImplicitReturns to false in your tsconfig.
Using this Task<T> class we can return data or values from a task. In Task<T>, T represents the data type that you want to return as a result of the task. With Task<T>, we have the representation of an asynchronous method that is going to return something in the future.
The Task<TResult> return type is used for an async method that contains a return statement in which the operand is TResult . In the following example, the GetLeisureHoursAsync method contains a return statement that returns an integer.
Explicitly sets the return value for a task. In a DAG of tasks, a task can call this function to set a return value. Another task that identifies this task as the predecessor task (using the AFTER keyword in the task definition) can retrieve the return value set by the predecessor task.
returning Task.CompletedTask
is cleaner.
public Task CreateAsync(ApplicationUser user)
{
if (user == null)
{
throw new ArgumentNullException("user");
}
Task.Factory.StartNew(() => { Console.WriteLine("Hello Task library!"); });
// other operations
return Task.CompletedTask;
}
Needs a return
:
return Task.Factory.StartNew(() => { Console.WriteLine("Hello Task library!"); });
Or better:
return Task.Run(() => { Console.WriteLine("Hello Task library!"); });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With