C # ValueTuple with one-time items

Let's say I have a method foothat returns a ValueTuple, where one of its members is one-time, for example (IDisposable, int).

What is the best way to ensure that the returned disposable object is correctly placed on the caller?

I tried the following:

using (var (disposable, number) = foo())
{
    // do some stuff using disposable and number
}

But this does not compile:

'(IDisposable disposable, int number)': the type used in the using statement must be implicitly converted to 'System.IDisposable'

Do I need to wrap my code in a try-finally block and explicitly delete my object in a finally block, or is there a better way to do this?

, , ValueTuples IDisposable Dispose() . Microsoft?

+4
1

using, :

var (disposable, number) = foo();
using (disposable)
{
    // do some stuff using disposable and number
}

, , - , using , . . , using , .


, , ValueTuples IDisposable Dispose() . Microsoft?

. , , ( ?!?!). , , .

, , , , .

. , , , . . , , : , , , , , - , .

+4

Source: https://habr.com/ru/post/1686416/


All Articles