0

I have a interface that must return Task type.

public interface IStrategy
{
    Task Test();
}

And this interface will be called by this line:

    IStrategy strategy = StrategyFactory.Generate();
    result = await strategy.Test();

And I need to implement it with no-op like this:

Option1

public class NoopStrategy:IStrategy
{
    public async Task Test()
    {
        return await Task.FromResult<obeject>(null);
    }
}

or I try to delete asyc, it also work:

Option2

public class NoopStrategy:IStrategy
{
    public Task Test()
    {
        return Task.FromResult<obeject>(null);
    }
}

So, I want to know which is better? and what's the different?

v11
  • 2,124
  • 7
  • 26
  • 54
  • http://stackoverflow.com/questions/19098143/what-is-the-purpose-of-return-await-in-c – Stavm Feb 16 '17 at 08:32
  • In my opinion, write your methods assuming that they won't be `async`. When you find yourself *needing* to write `await`, that's the time to make it `async`. – Damien_The_Unbeliever Feb 16 '17 at 08:44
  • @Damien_The_Unbeliever I just want to know whether is any wrong if some code await , but method doesn't have async flag. it can pass the compile. – v11 Feb 16 '17 at 08:47

0 Answers0