public async Task<Data> GetData()
{
Task<Data> data = null;
//This data will be fetched from DB
Data obj = new Data();
obj.ID = 1;
obj.Name = "Test";
//Need to
// data = obj;
return Task.Run(() =>
{
return obj;
});
}
Error 1 Since this is an async method, the return expression must be of type 'WebApplication2.Data' rather than 'Task' \inb-fs01\Users\user\Visual Studio 2012\Projects\WebApplication2\WebApplication2\Home.aspx.cs 35 20 WebApplication2
Can someone help me sorting out this issue?
Async methods have three possible return types: Task<TResult>, Task, and void. In Visual Basic, the void return type is written as a Sub procedure. For more information about async methods, see Asynchronous Programming with Async and Await (Visual Basic).
Async methods can have the following return types: Task, for an async method that performs an operation but returns no value. Task<TResult>, for an async method that returns a value. void , for an event handler.
DoSomething()' is an async method that returns 'Task', a return keyword must not be followed by an object expression. Did you intend to return 'Task<T>'?
The recommended return type of an asynchronous method in C# is Task. You should return Task<T> if you would like to write an asynchronous method that returns a value. If you would like to write an event handler, you can return void instead. Until C# 7.0 an asynchronous method could return Task, Task<T>, or void.
Where ever u are calling this function, try to put await before that call, like below:
Data obj = await GetData();
And return Data
object simply from GetData()
method.
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