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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T07:36:21+00:00 2026-05-18T07:36:21+00:00

How does the GC dispose objects created in the following 2 scenarios? 1) Private

  • 0

How does the GC dispose objects created in the following 2 scenarios?

1)

Private Function DoSomething() As Boolean
   Return New DatabaseManager().Insert()
End Function

2)

Private Function DoSomething() As Boolean
   Dim mngr As New DatabaseManager()
   Return mngr.Insert()
End Function

In Option 1, I don’t create local variable to hold the reference of the object. In Option 2, I hold the reference in local variable.

What option is better and why? (if any)

  • 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-18T07:36:22+00:00Added an answer on May 18, 2026 at 7:36 am

    As you can see, in the option 1, i dont create local variable to hold the reference of the object

    You might not be creating it explicitly but believe me at the IL level which is what the compiler emits there is an instance of the object created because you cannot call an instance method on an object without an instance.

    Those are equivalent in terms of garbage collection and probably the compiler will optimize it. And because they are equivalent of course the preferred is the one which involves less code and is more readable to you. For me it’s the first but it could subjective, so it’s really up to you.

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

Sidebar

Related Questions

How are the objects created or used in a P/invoke function or RCW native
Does anyone have a translate function for x/y positions after rotation in javascript? for
Does anyone know why UsernameExists wont return True. I must have my syntax messed
Does the following psuedo code accomplish my goal of cleaning up after myself when
I've got the following setup: public class SomeClass { private DirectoryEntry _root; private DirectorySearcher
I have the following code that uses Sequence objects to read data from a
I'm looking for a good way to dispose of a series of objects, so
When you create a Task while specifying TaskCreationOptions.LongRunning a new thread is created specifically
I am confused about garbage collection process on objects. object A = new object();
Is it true that this does not necessarily mean the stream has been disposed

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.