I am trying to master async method syntax in .NET 4.5. I thought I had understood the examples exactly however no matter what the type of the async method is (ie Task<T>
), I always get the same type of error error on the conversion back to T
- which I understood was pretty much automatic. The following code produces the error:
Cannot implicitly convert type 'System.Threading.Tasks.Task<System.Collections.Generic.List<int>>
' to 'System.Collections.Generic.List<int>
'
public List<int> TestGetMethod()
{
return GetIdList(); // compiler error on this line
}
async Task<List<int>> GetIdList()
{
using (HttpClient proxy = new HttpClient())
{
string response = await proxy.GetStringAsync("www.test.com");
List<int> idList = JsonConvert.DeserializeObject<List<int>>();
return idList;
}
}
It fails if I explicitly cast the result as well. This:
public List<int> TestGetMethod()
{
return (List<int>)GetIdList(); // compiler error on this line
}
somewhat predictably results in this error:
Cannot convert type 'System.Threading.Tasks.Task<System.Collections.Generic.List<int>>
' to 'System.Collections.Generic.List<int>
'
Any help greatly appreciated.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…