0

When VS sees that you are awaiting within a non-async method, it shows you an error.

When selecting to fix that error by adding async to the method signature - VS also changes void to Task (which then has to be manually fixed for event handlers). Is there a way to prevent that?




VS also appends "Async" to the method name. Is there a way to prevent that? (It was asked here Visual Studio 2017 force to add "async" at method name when use suggest (Ctrl + .) and it looks like we're waiting for MS to fix that. Perhaps it's already fixed?)

ispiro
  • 26,556
  • 38
  • 136
  • 291
  • There is a Quick Actions option `Make method async (stay void)`. It is below regular `Make method async` option. – Leonid Vasilev Jan 25 '18 at 18:13
  • I believe that currently you can't prevent name modification. There are no Quick Actions settings and there is no relevant rule in C# Code Style settings in Visual Studio. Although I understand the issue with `Async` suffix, in general it is recommended to use it with asynchronous methods. For more information check [Does the use of the “Async” suffix in a method name depend on whether the 'async' modifier is used?](https://stackoverflow.com/questions/15951774/does-the-use-of-the-async-suffix-in-a-method-name-depend-on-whether-the-async). – Leonid Vasilev Jan 25 '18 at 18:21
  • Using `async void` is generally a bad idea, except in limited situations: https://msdn.microsoft.com/en-us/magazine/jj991977.aspx – Pete Garafano Jan 25 '18 at 18:47
  • @PeteGarafano As you can see from the link you've supplied, event handlers are one of those exceptions. – ispiro Jan 25 '18 at 18:54
  • @LeonidVasilyev I didn't notice that! You can transform your comment into an answer. That's it! – ispiro Jan 25 '18 at 21:11

1 Answers1

2

To leave void as a return type use a Quick Actions option Make method async (stay void). You can't prevent name modification.

Make method async (stay void) Quick Action animation

There are no Quick Actions settings and there is no relevant rule in C# Code Style settings in Visual Studio. EditorConfig required_suffix doesn't help either. Although I understand the issue with Async suffix, in general it is recommended to use it with asynchronous methods. For more information check:

Leonid Vasilev
  • 11,910
  • 4
  • 36
  • 50