-4

I have this code:

Method1(Method2());

However, Method2 returns an object that needs to be disposed. Here is how I can handle this:

using (var x = Method2())
{
    await Method1(x);
}

Method1 and Method2 belong to framework and it is not up to me to change them.

I have number of such cases. For example, Method2 creates HTTP request and Methid1 sends it.

Again, both methods belong to library that I cannot change.

I know if I do not dispose object, Garbage Collector will eventually do this. May be not soon. I am wandering, if may be in case when there is no any variable that references the object (as it will be after Method1 returns), I can count on Garbage Collector to dispose the object immediately, and thus it is ok to use the original short option.

polina-c
  • 6,245
  • 5
  • 25
  • 36
  • It makes is hard to give advice with meaningless names like `Method1` and `Method2`. With more meaningful names, it might be possible to give alternative options like to change `Method1` to take a `Func` instead, and thus do the `using` inside of `Method1`. – mjwills Jan 20 '19 at 23:59

1 Answers1

2

Use the using statement, just like you have shown, yes it is the elegant way

using (var something = Method2())
{
   Method1(something);
}

Or if you will

using (var something = Method2())
   Method1(something);

Anything else would be unusual and confusing (ie disposing it in your method1)... As pointed out by the comments, unless this was some sort of Command/Query Service, or other Dependency that could be injected with a disposable scope

Also, calling wait on anything is suspicious these days

TheGeneral
  • 79,002
  • 9
  • 103
  • 141
  • This is back to front, and how is it "more elegant" then OPs second block of code? – DavidG Jan 21 '19 at 00:19
  • @DavidG whats back to front ;) – TheGeneral Jan 21 '19 at 00:20
  • And now it's identical to the original code. How does this help? – DavidG Jan 21 '19 at 00:20
  • @DavidG Its clarification, that there is no more elegant way and the only elegant way – TheGeneral Jan 21 '19 at 00:21
  • "elegant" is purely opinion based though. Wouldn't DI be a "more elegant" method? – DavidG Jan 21 '19 at 00:22
  • @from the code shown, this is not a Command/Query, nor a service, DI'ing every method that needs to be disposed is not elegant, it would be as strange as it would be insidious for the next developer to refactor. The using statements purpose in life is exactly this, to dispose, it was added for this purpose. Also thanks for the down vote i am back to multiples of 5 unless this question gets deleted – TheGeneral Jan 21 '19 at 00:24
  • @DavidG, why do you think the question is bad? I am searching for more elegant way, because I would like to avoid using extra variable and make the code more readable. It seems the answer is "there is no more elegant way and extra variable has to be here". May be, in future .NET will be able to suggest something and the answer will appear here. Anyway, what makes the question bad? – polina-c Mar 21 '19 at 22:12