简体   繁体   中英

Using Task.FromResult to implicitly convert a Task<T> to a Task<X> where T : X?

Task<IDictionary<double, double>> GetTaskDict()
{
     return Task.FromResult(new Dictionary<double, double> () );
}

This code does not compile because we can not convert between Task<Dictionary<double, double>> to Task<IDictionary<double, double>>. Why does this not work, and is it possible to make this type of call work? This opposed to a method like this which compiles

IDictionary<double, double> GetTaskDict()
{
     return new Dictionary<double, double> ();
}
return Task.FromResult<IDictionary<double, double>>(new Dictionary<double, double>());

This is because Task<T> is not covariant, so a Task<Foo> is not interchangeable with a Task<IFoo> even if Foo: IFoo .

You may also wish to consider using ValueTask<T> if you're going to have lots of results which are available synchronously - it is significantly cheaper in that scenario. You can even use the implicit new() usage in that scenario:

ValueTask<IDictionary<double, double>> GetTaskDict()
{
    return new(new Dictionary<double, double>());
}

(here, new(...) is interpreted as new ValueTask<IDictionary<double, double>>(...) from the declared return-type of the method)

Suppose it were possible.

Then someone could do this:

Task<IDictionary<double, double>> myTaskDict = new Task<Dictionary<double, double>>();

Task.SetResult(myIDictionaryWhichIsNotActuallyADictionary);

In other words, as @MarcGravell has said, Task is not covariant, and for very good reason.


You can specify the type explicitly like this though:

Task<IDictionary<double, double>> GetTaskDict()
{
     return Task.FromResult<IDictionary<double, double>>(new Dictionary<double, double>());
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM