C
C#4w ago
Jacko

How can you tell if an object supports using?

I’m following some azure docs and it uses the code using(var cache = ConnectionMultiplexer.Connect()) I understand that it frees all the caches resources and closes the connection when it’s finished but I’m wondering if given a random class how do you know if it supports that type of syntax? Is it in the function signature or would you have to read the code to see if it defines a dispose function?
2 Replies
ero
ero4w ago
the return type must implement IDisposable or IAsyncDisposable (used with await using)
IsNotNull
IsNotNull4w ago
The only other exception to requiring those interfaces is for ref structs, but typical developers will rarely need to use ref structs:
No description

Did you find this page helpful?