1

Sorry for a terrible title, I just wanted to confirm that I am making the correct assumptions.

Say I have 3 methods that are simplified to:

public async Task Method1()
{ 
    var obj = await Method2();
    //do work on obj
} 

public Task<object> Method2()
{
    //do some work
    return Method3();     
}

public async Task<object> Method3()
{
    //do async work
    return obj;
}

Method2 above does no async work internally, but is in my async stack. It could be rewritten as:

public async Task<object> Method2()
{
    //do some work
    return await Method3();
}  

Am I correct that, as Method2 doesn't actually do async work, that it is better to leave it as a non-async method? My thought is that I'm saving the overhead of creating an extra state machine to hold the thread's resources when it is not needed.

Jonesopolis
  • 25,034
  • 12
  • 68
  • 112

1 Answers1

0

Correct. You can return the Task directly from a method without adding the async/await keywords if the method does not need to await any result (ie. consume the result of a task or wait for a task to complete to be able to do some execution). That returned Task can then be awaited by a caller further down the call stack. This can save a little overhead and make for slightly less code (although that amount of code is actually trivial).

You often see this with pass through or convenience methods.

Igor
  • 60,821
  • 10
  • 100
  • 175