Reputation: 43
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> ();
}
Upvotes: 3
Views: 194
Reputation: 71534
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>());
}
Upvotes: 2
Reputation: 1062770
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)
Upvotes: 6