2

Say you wrap the entity framework DbContext SaveChangesAsync(). So the original method returns Task<int>.

So when wrapped, is there any difference between:

public Task<int> SaveChangesAsync()
{
    return dbContext.SaveChangesAsync();
}

and

public async Task<int> SaveChangesAsync()
{
    return await dbContext.SaveChangesAsync();
}
Callum Linington
  • 14,213
  • 12
  • 75
  • 154
  • There is a slight difference in that with `Task.Run` you could have unhandled exceptions, but with this code, it will be awaited at some point. So I don't think I could accept that as a duplicate – Callum Linington Jun 22 '15 at 08:39
  • See the edit on the question and the code in the answers, the `Task.Run` is irrelevant – Lukazoid Jun 22 '15 at 08:41
  • @Lukazoid sorry I still can't see the duplication – Callum Linington Jun 22 '15 at 08:43
  • In both cases, the question is "is there a difference between returning a `Task` directly and returning the `await`ed `Task`?". Still, http://stackoverflow.com/questions/23536848/return-task-or-await-and-configureawaitfalse?rq=1 seems like a better fit for a duplicate. – Luaan Jun 22 '15 at 08:47
  • I would agree with you @Luaan that is far closer. – Callum Linington Jun 22 '15 at 08:52
  • Didn't realise I had originally flagged the possible duplicate as a possible duplicate of this: http://stackoverflow.com/questions/19098143/what-is-the-purpose-of-return-await-in-c which may be another better option. Hopefully one of these many possible duplicates may answer your question :) – Lukazoid Jun 22 '15 at 08:57

0 Answers0