Convert to 'using' declaration (extends resource lifetime)
There are two conversions enabling the new c# 8.0 feature of using declarations. One is straight forward:
Convert to 'using' declaration
The other is less straight forward:
Convert to 'using' declaration (extends resource lifetime)
Well it is clear what is meant by it, but not how bad that is.
Take the following example:
await using (SqlDataReader reader = await SqlHelperAsync.ExecuteReaderAsync(Config.DbConnectionString, CommandType.Text, sqlQuery))
{
while (reader.Read())
{
.....
}
}
return Answers;
Converting this to a 'using' declaration extends the lifetime of the resource until after the return Answers. But how bad is that?
Is there some guidance when a using declaration should not be used in view of the resource lifetime?
Please sign in to leave a comment.
Hello Pieter,
Well generally you shouldn't extend resource lifetime if the code below the
using
statement depends on the resource disposal.Let's say you have the following code:
If you turn this code into:
then it may not be working as before because code performing another file operation expects the file to be released after performing first file operation.
Thank you.
Thank you, that is clear.
But there are no performance or garbage collection considerations to worry about?
Angelina Elycheva
So why am I getting a R# warning about stacked usings?
Resharper wants to change this...
Which seems to be counterintuitive to both me and your description.
Hi Andrew,
It is a known issue RSRP-482303.
Please vote or leave a comment to subscribe to the issue and receive updates.