Note that there are some explanatory texts on larger screens.

plurals
  1. POWhen is disposed an object in a functional approach?
    text
    copied!<p>I am puzzling with this scenario. Consider the following basic code:</p> <pre><code>class Program { /// &lt;summary&gt; /// Create a function that sums up any of the ints supplied /// &lt;/summary&gt; private static Func&lt;IEnumerable&lt;int&gt;, Func&lt;int&gt;&gt; Test = (coll) =&gt; { //create the function Func&lt;int&gt; fn = () =&gt; { return coll.Sum(); }; return new Func&lt;int&gt;(fn); }; static void Main(string[] args) { //given an ints set, create a function that sums up any of them int[] coll = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; Func&lt;int&gt; fn = Test(coll); //same as before: different set, and another func's instance coll = new[] { 5, 5, 5, 5 }; Func&lt;int&gt; fn2 = Test(coll); //dispose any pending object coll = null; GC.WaitForPendingFinalizers(); //evaulate both the functions in a lazy fashion Console.WriteLine(fn()); //yields 45 Console.WriteLine(fn2()); //yields 20 Console.Write("Press any key..."); Console.ReadKey(); } } </code></pre> <p>The purpose is absolutely useless, but I am asking myself when the two integer arrays will be disposed. The "Test" function should return another function, that is not evaluated until will be called. That is verified by using the debugger. So far, the first "coll" array should be disposed because it is replaced by the new set. However, the first function still evaluates correctly. At this point, either I must wait a longer time for the GC, or the array will be pinned somewhere...in the second hypothesis, I'd expect the arrays would never released. Where is my error? Thanks a lot in advance.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload