Let's say I have a method foo
which returns a ValueTuple
where one of it's members is disposable, for example (IDisposable, int)
.
What is the best way to make sure the returned disposable object is correctly disposed on the calling side?
I tried the following:
using (var (disposable, number) = foo())
{
// do some stuff using disposable and number
}
But this won't compile:
'(IDisposable disposable, int number)': type used in a using statement must be implicitly convertible to 'System.IDisposable'
Do I really need to wrap my code in a try-finally block and dispose my object explicitly in the finally block, or is there a nicer way to do it?
Actually I'd like to see the new ValueTuples implement the IDisposable
interface and call Dispose()
on all their disposable members. Could this be a worthwhile feature request for Microsoft?
In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.
C is a structured, procedural programming language that has been widely used both for operating systems and applications and that has had a wide following in the academic community. Many versions of UNIX-based operating systems are written in C.
C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.
C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...
Just move the method call outside of the using
statement and then just use the disposable object:
var (disposable, number) = foo();
using (disposable)
{
// do some stuff using disposable and number
}
The reason your version didn’t work is simply because whatever the expression inside the parentheses of the using
results in needs to be disposable but the value tuple itself is not disposable. So you just need to split this up. Fortunately, the using
statement is not required to construct the object within that expression, you can just pass any existing object to it.
Actually I'd like to see the new ValueTuples implement the
IDisposable
interface and callDispose()
on all their disposable members. Could this be a worthwhile feature request for Microsoft?
By that logic, all collections would be have to do that. For example disposing a list should dispose all its members, disposing a dictionary should dispose all its values (and keys?!?!). And when you have a type just using a simple list, that object would also need to be disposable.
So basically, you would end up spreading that and end up with a lot of objects that are suddenly disposable although they don’t actually have any actual resources that require it.
Making objects disposable shouldn’t be done lightly. Usually, objects creating a disposable object become responsible for properly disposing the object later, they own the lifetime of the object. But for collections, this is very often not the case. Collections are usually just that: Collections to hold on to objects, but that does not say anything about whether or not they are owned by the collection—most of the times they are owned by the object that also created the collection, so that object should then at some point dispose the objects by simply looping through the collection.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With