ITask icon indicating copy to clipboard operation
ITask copied to clipboard

Covariant return types with ITask

Open MgSam opened this issue 1 year ago • 0 comments

Not really an issue per say, just wanted to suggest that you update your documentation to reflect that this also works really well with covariant return type support that was added in C# 9.0.

Here's a small example if you want one for your tests or docs:

[TestFixture]
public class MyTests
{
    [Test]
    public async Task MyTest()
    {
        var pz = new PettingZoo();
        var an = await pz.GetAnimal().ConfigureAwait(false);
        Assert.AreEqual("Cat", an.Name);
    }
}

public class Zoo
{
    public virtual async ITask<Animal> GetAnimal()
    {
        await Task.Delay(0).ConfigureAwait(false);
        return new Animal();
    }
}

public class PettingZoo : Zoo
{
    public override async ITask<Cat> GetAnimal()
    {
        await Task.Delay(0).ConfigureAwait(false);
        return new Cat();
    }
}

public class Animal
{
    public string Name { get; set; } = "Animal";
}

public class Cat : Animal
{
    public Cat()
    {
        Name = "Cat";
    }
}

MgSam avatar Jan 20 '23 21:01 MgSam