0

I have little mess up things with tasks. So please explain me which is the main difference between a Task with signature vs no-signature. Example

 async Task myTaskAsync(); vs async Task myTask(); 

Is there any difference if i will not include Async in my end of fuction?

9
  • 1
    No, it is just a naming convention. You can name it as you wish Commented Nov 15, 2018 at 8:27
  • 1
    No difference. It is just recommended to include it so you can spot immediatly it's awaitable / async (TPL). Commented Nov 15, 2018 at 8:27
  • Thank you for the asnwer. One last question, is it better to use async task instead async void? Commented Nov 15, 2018 at 8:31
  • async void is for EventHandlers. blog.stephencleary.com/2012/02/async-and-await.html => See section "Return Types" Commented Nov 15, 2018 at 8:31
  • 2
    The reason why you want to name it with Async is just for your knowledge or other developers who may inherit your code some day. No other purpose for the naming. As best practice, always name your functions appropriately. Commented Nov 15, 2018 at 8:31

2 Answers 2

2

It does not matter regarding the behavior of the function if you add the "Async" suffix or not.

It does matter regarding the perception of your API by clients.

This naming convention is broadly adopted, so you do yourself and others a favor using it.

BTW: If that API in question already contains legacy async functions (non-TAP) that are named "xxxAsync", then it is recommended to use "xxxTaskAsync" instead.

Sign up to request clarification or add additional context in comments.

1 Comment

A similar question has been answered here: stackoverflow.com/a/24627122/982149. It's not exactly a dupe, but may be interesting for you. It includes a statement by Steven Toub himself.
1

Yes, it matters when you care about your code design, for example:

public interface IMyService { Task<int> GetWeightAsync(); int GetWeight(); } 

In this case you support both: async and non-async version for users of this interface, so they can decide which path they want to choose.

C# is designed polymorphic, but in case of async, it is just way to fake it. The compiler will still complain if signature will be same except return value, so people decided to add suffix 'Async' at the end.

4 Comments

It is still just a convention. Naming it "Herbert()" won't change anything (but the name).
Oh, the name in programming means more than logic itself. It often describes logic, so if you name it Herbert() you will purposely name it so it contains something related to Herbert. Just like we use Async to describe behavior of polimorphic function (because we need to avoid compilation error on linkage).
But it will not behave differently. That's what I am saying.
Yes, name will not define behavior, as I said, it's just one of necessity of C# when method just needs to be with different name. In fact it's name should be the same and compiler should have decided itself what to choose and when, but...real world, we line in it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.