❔ ✅ Lifetime of variable referenced in Func<>
In some of my tests, I'm creating tables as I go, and then dropping the tables at the end of the test fixture.
I'm doing this by having a
RegisterType
function that looks like this:
And then in my TearDown function, I iterate over _dropTableFunctions
and call each one.
My question here is not related to the database side of this, but is to ask whether I'm risking the Database
objects having been destroyed by the time these functions are called? Are references from a function like this considered proper references to the variable, or should I also be storing reference to each database
instance elsewhere to ensure that it is kept alive?6 Replies
the compiler will capture the local vars that you use
in this case
database
so yes, they are "proper references"Good stuff - thanks. I wanted the check that I wasn't potentially introducing an intermittent failure here!
Appreciate the clarification
You can see how the C# compiler lowers this to use a display class
https://sharplab.io/#v2:D4AQTAjAsAULITAAgLIAoCUSDesn6QEsA7AFyQEMAjAYyQF4kIkBqJMAbljwJABYkAZQD2AWwCmKcaQAWwgCaYcPArwgBONNRoYuMVQF9u+3gHYhYydLny9BoA==
SharpLab
C#/VB/F# compiler playground.
is converted into
👍
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.