11

APM uses BeginXXX/EndXX pairs and the Event-based Asynchronous Pattern (EAP) uses XXXAsync and XXXCompleted pairs, but I haven't seen anything standard on how to name methods that return a task.

I have been using XXXTask:

Data GetData() 
Task<Data> GetDataTask()

but was wondering if a more standard approach has developed

Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
Karg
  • 1,467
  • 3
  • 15
  • 18
  • see also https://stackoverflow.com/questions/15951774/does-the-use-of-the-async-suffix-in-a-method-name-depend-on-whether-the-async – Tim Abell Mar 25 '19 at 13:46

3 Answers3

14

For C# 5.0 (with .NET 4.5), the naming convention is XXXAsync for task returning methods.

If there already exists a method with this naming (for instance, on the WebClient already has a DownloadDataAsync method that implements the EAP pattern), then the Task returning async method should be named XXXTaskAsync.

Karg
  • 1,467
  • 3
  • 15
  • 18
  • Here is the link to the official documentation of this naming convention: http://msdn.microsoft.com/en-us/library/hh873175.aspx. – Marc Sigrist Apr 06 '13 at 15:03
  • On a side note: Make sure that if you use that naming convention, you also adhere to the other principles described in the above link. E.g. "Synchronous work should be kept to the minimum" might not necesarrily be obvious when your Async method just returns the Task from another method it called half way through the function. – Matthijs Wessels Apr 18 '13 at 12:03
  • Is `BeginXXX` frowned upon? I personally prefer the latter over `XXXAsync`. –  Mar 27 '17 at 14:56
2

I'd recommend using the patterns in the ParallelExtensionsExtras library since that's done by the same team that made the TPL in the first place :)

Link

Their pattern seems to be the same as yours: [SyncAction]Task for the method that does SyncAction async via a Task (which is returned) - DownloadDataTask, SendTask, etc.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
James Manning
  • 13,429
  • 2
  • 40
  • 64
  • Accepted, not for agreeing with me, but for providing the reference. Thanks! – Karg Jun 04 '10 at 14:39
  • 1
    I have to admit, the naming actually seems a little odd to me in terms of the goal that method names should read like an action - BeginFoo, EndFoo are pretty clear (as names), and FooAsync is even decent IMHO as async seems to clearly be an adverb. However, FooTask is actually kind of odd to me in that the 'foo' verb has a noun just tacked onto the end. IMHO it is then confusing, as, for instance "SendTask" as a method name makes me thing it's something that is going to Send a Task. I'm not sure "FooAsTask" or "FooAsyncAsTask" are actually better, though - so theirs is fine with me. :) – James Manning Jun 04 '10 at 15:53
-2

You may consider to provide a Property instead of a GetXXX-Method, which is more usual in C#. You could then write

Task<Data> DataTask { get; set; } //auto-implemented
Simon
  • 9,255
  • 4
  • 37
  • 54
  • 1
    I think this is somewhat orthogonal to the Task version - GetData() being a method instead of a property would be the original choice, and given that it's heavyweight enough to create an async version, it sounds like that's the right choice :) The *Task version also being a method certainly makes sense and matches the pattern done in the ParallelExtensionsExtras library (although one could argue the choice wasn't there since there is no support for extension properties :) Methods vs. properties guidelines for others that run across this: http://msdn.microsoft.com/en-us/library/ms229054.aspx – James Manning Jun 03 '10 at 03:19