Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Summary on async (void) Method: What to return?

This is maybe a trivial question but currently im doing some Inline-Documentation for future Coworkers and stumbled upon something like that:

/// <summary> /// This Class is totaly useless /// </summary> public class DummyClass {    /// <summary>   /// Will do nothing   /// </summary>   public void DoNothing() {   }    /// <summary>   /// Will do nothing async   /// </summary>   /// <returns></returns> <---- What to write here?   public async Task DoNothingAsync() {     await Task.Run(() => { });   }  } 

As you might know, typing 3 slashes above an Method/Field/Class/whatever, triggers VisualStudio to perform its Summary-Snippet-Completion.

Question

Is Task actually a valid return-value? And if so, what do i write in the <returns></returns>?

I surely do know, i can ignore this, but for the sake of completeness im willing to write stuff there.

like image 580
lokusking Avatar asked Oct 06 '16 06:10

lokusking


People also ask

Can we return void in async method?

In short, if your async method is an event handler or a callback, it's ok to return void .

What does an async method return?

Async functions always return a promise. If the return value of an async function is not explicitly a promise, it will be implicitly wrapped in a promise. Note: Even though the return value of an async function behaves as if it's wrapped in a Promise.resolve , they are not equivalent.

How does async void work?

async void has the same semantics as async Task , except for exceptions. An async void method will capture the current SynchronizationContext at the beginning of the method, and any exceptions from that method will be captured and raised directly on that captured context.

Is async void fire and forget?

The async void case is a “fire and forget”: You start the task chain, but you don't care about when it's finished. When the function returns, all you know is that everything up to the first await has executed. Everything after the first await will run at some unspecified point in the future that you have no access to.


1 Answers

If we take inspiration from API's that Microsoft have produced recently, you might just state:

<returns>No object or value is returned by this method when it completes.</returns> 

I dislike "A task object that can be awaited" for the same reason I wouldn't decorate a method that returns an int with "an integer which can be compared to zero or used in mathematical operations" - it doesn't describe the return value of the method, it describes the type. The type has its own documentation which can be consulted.

like image 52
Damien_The_Unbeliever Avatar answered Oct 01 '22 17:10

Damien_The_Unbeliever