Cannot implicitly convert type from Task<>

后端 未结 3 743
逝去的感伤
逝去的感伤 2020-12-01 08:56

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

3条回答
  •  星月不相逢
    2020-12-01 09:37

    You need to make TestGetMethod async too and attach await in front of GetIdList(); will unwrap the task to List, So if your helper function is returning Task make sure you have await as you are calling the function async too.

    public Task> TestGetMethod()
    {
        return GetIdList();
    }    
    
    async Task> GetIdList()
    {
        using (HttpClient proxy = new HttpClient())
        {
            string response = await proxy.GetStringAsync("www.test.com");
            List idList = JsonConvert.DeserializeObject>();
            return idList;
        }
    }
    

    Another option

    public async void TestGetMethod(List results)
    {
        results = await GetIdList(); // await will unwrap the List
    }
    

提交回复
热议问题