Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET 4.5 async await and overloaded methods

I have an async method:

public async Task<UserLoginExResult> LoginExAsync(CustomTable exRequest, string language, bool throwEx = true) {     UserLoginExResult result = await UserService.LoginExAsync(UserGroup, language, TimezoneOffset, GetDeviceInfo(), GetLoginProperties(), exRequest);      ProcessLoginResult(result, false, throwEx);      return result; } 

And an overload:

public Task<UserLoginExResult> LoginExAsync(CustomTable exRequest, bool throwEx = true) {     return LoginExAsync(exRequest, Language.ID, throwEx); } 

I'm not sure if I should mark the overloaded one (the one with fewer parameters) as async and use await? I guess I should not but can you tell me what whould happen if I would do that? I'm quite lost in here and not really sure what Task it would wait for? Would it create an extra Task or await doesn't create a new Task?

like image 879
JakubRi Avatar asked Oct 26 '12 11:10

JakubRi


1 Answers

No, there's little benefit in using an async method when it's just going to wrap and unwrap an existing one. It's fine to just have a "normal" method here, delegating to the "real" async method.

(They're not quite the same - for example, if the UserService.LoginExAsync method throws an exception rather than returning a failed task, the async/await version will just return a failed task, whereas the other version will also throw immediately.)

like image 155
Jon Skeet Avatar answered Sep 24 '22 01:09

Jon Skeet