Cannot implicitly convert type from Task<>

C#Async Await

C# Problem Overview


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.

C# Solutions


Solution 1 - C#

The main issue with your example that you can't implicitly convert Task<T> return types to the base T type. You need to use the Task.Result property. Note that Task.Result will block async code, and should be used carefully.

Try this instead:

public List<int> TestGetMethod()  
{  
    return GetIdList().Result;  
}

Solution 2 - C#

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

public Task<List<int>> TestGetMethod()
{
    return GetIdList();
}    

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;
    }
}

Another option

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

Solution 3 - C#

Depending on what you're trying to do, you can either block with GetIdList().Result ( generally a bad idea, but it's hard to tell the context) or use a test framework that supports async test methods and have the test method do var results = await GetIdList();

Solution 4 - C#

I just had the same issue pop up but the resolution was different than the others. I'm working with two async calls within an async method, this code is what was giving me the error:

var fileContents = reader.ReadToEndAsync();
if (fileContents != null)
{
     var profile = await Task.Run(() => JsonConvert.DeserializeObject<T>(fileContents));
     return profile;
}

This is the fix. I had forgotten the async on the first line:

var fileContents = await reader.ReadToEndAsync();
if (fileContents != null)
{
     var profile = await Task.Run(() => JsonConvert.DeserializeObject<T>(fileContents));
     return profile;
}

The error message was showing on "fileContents" on the line var profile = ... So it wasn't immediately obvious of where the error was.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPaulRView Question on Stackoverflow
Solution 1 - C#user2388853View Answer on Stackoverflow
Solution 2 - C#MayankView Answer on Stackoverflow
Solution 3 - C#James ManningView Answer on Stackoverflow
Solution 4 - C#ZonusView Answer on Stackoverflow