using parameters #9021
Replies: 5 comments 17 replies
-
What's wrong with this? public async void TalkTo(IDisposable client)
{
using (client)
{
// Have an amazing conversation
}
} |
Beta Was this translation helpful? Give feedback.
-
Did you try IDisposableAnalyzers? It might not be the answer for what you are trying to do, but it has helped me managing my disposable objects. |
Beta Was this translation helpful? Give feedback.
-
In your example the above shouldn't throw |
Beta Was this translation helpful? Give feedback.
-
This feels like a pretty backward use of |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Currently, when you pass a disposable object into a function there is no way to tell if it is disposed or not.
However, to know whether something is disposed after being passed into a method, you would need to recursively track the object after that method call boundary. Much like with nullable values. For nullable values, this issue was solved by adding an optional
?
to the type. That way, you don't need to look further than the signature of the method being called.For
IDisposable
objects there is no such solution currently. After it's passed into a method, the code analyzer would have to recursively check if the object is disposed or not. So instead, solutions like IDisposableAnalyzers just suggest you never dispose anything that you got from a parameter.To provide a solution, I would like to propose:
Beta Was this translation helpful? Give feedback.
All reactions