0

I am a beginner in C# and do not have a full grasp on async await functionalities. But from what i understand an async method should have "async" in its definition and return a task. Now the AcceptAsync method has an overload that returns a bool. How is this asyncronous? Isn't this event based pattern? If so, is it the same/correlated to asyncronous programming?

  • It might actually use the async keyword, but it won't show up in intellisense or when you F12 and inspect the definition. Excluding the async keyword doesn't prevent the method from not being asynchronous. – gunr2171 Dec 02 '21 at 22:00
  • It is also missing on msdn(Third overload, with EventArgs): https://learn.microsoft.com/en-us/dotnet/api/system.net.sockets.socket.acceptasync?view=net-6.0. Also, when i try to await it, i get a compilation error, so it's not just intellisense – Ristea Stefan Dec 02 '21 at 22:11

2 Answers2

1

If you look at the documentation for Socket.AcceptAsync(System.Net.Sockets.SocketAsyncEventArgs), you'll see that the overload you're referring to exists since .NET Framework 2.0, and the rest of the overloads exist since .NET 6.0.

The Task Parallel Library (TPL) was introduced in .NET Framework 4.0 and async/await in .NET Framework 4.5. From this, it should be obvious that a .NET Framework 2.0 method does not use async nor Task nor follows conventions established several years later.

Regardless, Async as a suffix for methods that return Task (or equivalent) is a "best practice", not a requirement in any form. Asynchronicity in the TPL should be determined by the method returning Task/Task<T>/ValueTask/ValueTask<T>, the rest (whether it actually is asynchronous or not) are implementation details.

Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
-1

But from what i understand an async method should have "async" in its definition and return a task.

Nope. They should return a task, but they may well by sync if they call methods internally that take no sensible time. They only have to be async if they AWAIT something INTERNALLY. If they do not (i.e. because they call another method) they themselves gain nothing from being async.

TomTom
  • 61,059
  • 10
  • 88
  • 148
  • How about the third overload here: https://learn.microsoft.com/en-us/dotnet/api/system.net.sockets.socket.acceptasync?view=net-6.0 . It only retuns a bool. Not a Task. And if it is not awaitable, and may as well be sync if it was, why is it called async? What am i missing? – Ristea Stefan Dec 02 '21 at 22:07
  • Leftover from old times? Note the SocketAsyncEventArgs - it is an async method, just not folloiwing the async/await pattern. – TomTom Dec 02 '21 at 22:11