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 586857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:09:02+00:00 2026-05-13T15:09:02+00:00

For example, in the below code an ‘image’object will be created and then garbage

  • 0

For example, in the below code an ‘image’object will be created and then garbage collected at some unknown point in the future

void MyFunction() {

    Bitmap image = RetrieveImage();
    DoSomething(image);
}

What about

void MyFunction() {

    DoSomething(RetrieveImage());

}

In this case is the object garbage collected once it moves out of scope i.e. After the end of MyFunction. If not is there somewhere to enforce this?

  • 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-13T15:09:02+00:00Added an answer on May 13, 2026 at 3:09 pm

    No. In fact, you don’t really want it to be garbage collected – prompting the garbage collector very frequently will reduce performance.

    What you do want is to dispose of the unmanaged resources in a timely manner – and that’s where IDisposable comes in, along with the using statement:

    void MyFunction()
    {
        using (Bitmap image = RetrieveImage())
        {
            DoSomething(image);
        }
    }
    

    That will call image.Dispose() as it leaves the using statement, whether or not DoSomething threw an exception.

    You do have to use the extra variable though – unless you change DoSomething to take a Func<Bitmap> instead, so instead of:

    void DoSomething(Bitmap image)
    {
        // Code here
    }
    ...
    DoSomething(RetrieveImage());
    

    you’d have:

    void DoSomething(Func<Bitmap> imageProvider)
    {
        using (Bitmap image = imageProvider())
        {
            // Code here
        }
    }
    ...
    DoSomething(() => RetrieveImage());
    

    Note that that doesn’t give the opportunity to pass in a bitmap without it being disposed – which could be a problem if you want to use it again later. Still, it’s a nice technique to at least know about.

    EDIT: As mbeckish has pointed out in his comments, there isn’t very much benefit here over just disposing of the bitmap within RetrieveImage. Here’s a variant on the pattern though:

    public void ApplyToEachLineInFile(string file, Action<string> action)
    {
        using (TextReader reader = File.OpenText(file))
        {
            string line;
            while ((line = reader.ReadLine()) != null)
            {
                action(line);
            }
        }
    }
    

    Here the “acquire and dispose” logic is encapsulated, without the caller worrying about it – but the caller can still be very flexible in terms of the complexity of logic they pass in.

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

Sidebar

Related Questions

I constantly find myself writing similar code like the example below: if (object[Object Name]
I have created an object literal using the code below. Everything works fine. However,
The example code below works as as a server process. But when I add
For this code example below, usually I would use [self fall]; instead of the
The example below shows the code I am using to test whether a user
I know how to do this... I'll give example code below. But I can't
UPDATE 2011.09.13 This bug has been resolved by Adobe. The example code below now
Given the example source code below, is it possible for someone to see the
In the example below, if client code using GetPeople wanted to print the name
For the example below: if a == 100: # Five lines of code elif

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.