Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6053391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:02:06+00:00 2026-05-23T08:02:06+00:00

I am puzzling with this scenario. Consider the following basic code: class Program {

  • 0

I am puzzling with this scenario.
Consider the following basic code:

class Program
{
    /// <summary>
    /// Create a function that sums up any of the ints supplied
    /// </summary>
    private static Func<IEnumerable<int>, Func<int>> Test = (coll) =>
    {
        //create the function
        Func<int> fn = () =>
        {
            return coll.Sum();
        };

        return new Func<int>(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<int> fn = Test(coll);

        //same as before: different set, and another func's instance
        coll = new[] { 5, 5, 5, 5 };
        Func<int> 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();
    }

}

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T08:02:06+00:00Added an answer on May 23, 2026 at 8:02 am

    While your original reference, coll, is set to null, the functions fn & fn2 have a reference to the same int[] array.

    Therefore, the array that coll references will not be garbage collected until there are no more references to it, including the arguments to fn and fn2.

    I know the parameter within Test is a copy, but it’s a copy of the reference to the array object, not a copy of the array itself.

    The terms I used may not be correct. But hopefully this helps explain it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Alright, this one's been puzzling me for a bit. the following function encodes a
I'm getting this error... What is puzzling me is the fact that i do
This one has been puzzling my for some time now. Let's imagine a class
The following problem has been puzzling me for a couple of days (nb: this
I find the behaviour of this piece of code puzzling, why is the constructor
this site : http://www.drbob42.com/delphi/wizards.htm showed a very puzzling code at the bottom unit ShareMem;
I've been playing around with the MapKit and came across a puzzling scenario that
I haven't used SVN for all that long, but this one is puzzling me.
Take this sample code: Class Foo ReadOnly name As String Public Sub New(name As
I've been puzzling over this for a few days... feel free to shoot down

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.