Shared method to return string only if needed

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Shared method to return string only if needed



I have a method that is shared between two other methods which do the same thing, both post events to outlook calendar but in one case I don't need the response string returned, here is the method:


private static async Task<string> InitPost(Kalender kalender, string email)

using (Client)

var postUri = $"https://graph.microsoft.com/v1.0/users/email/calendar/events";
var httpContent = new StringContent(JsonConvert.SerializeObject(kalender), Encoding.UTF8, "application/json");
var method = new HttpMethod("POST");
var request = new HttpRequestMessage(method, postUri)

Content = httpContent
;
var response = await Client.SendAsync(request);
var responseString = await response.Content.ReadAsStringAsync();
return responseString;




Is it possible to call the method like so: await InitPost(KalenderEntity, email); or does it have to be var e = await InitPost(KalenderEntity, email);?


await InitPost(KalenderEntity, email);


var e = await InitPost(KalenderEntity, email);



if possible, can someone explain what happens with the return string? I am new to coding and what I have learned in school is that if you have a method with a return type then you have to "catch" it.





related: stackoverflow.com/questions/36757545/…
– rene
Aug 8 at 7:47




2 Answers
2



No, you don't need to "catch" the returned value, it's fine to ignore the return value of the method. But, in some cases this might be a warning sign of a bad design that is, if it's possible that the two callers, are using the same method for two different things, you should split your method into two different implementation.





You're right and this way it's more inline with SLOID principles, I will split the method. Thanks!
– Ako
Aug 8 at 8:22



you can leave await InitPost(KalenderEntity, email); if you don't need a result, it's ok. In this case, you don't assign the result of this method to any variable and it will be removed from memory by GAC.


await InitPost(KalenderEntity, email);





GAC inspects randomly right? It's gonna be a problem for performance in that case, I think I will follow Mahmouds answer and split the method. Thanks!
– Ako
Aug 8 at 8:22





I guess that there won't be performance problems, any way you allocate memory inside your method's body. But it's better to do as Mahmoud said for better code structure.
– kord
Aug 8 at 8:28






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS