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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:01:49+00:00 2026-05-28T06:01:49+00:00

I was looking at someone else’s code and wondering why anyone would do it

  • 0

I was looking at someone else’s code and wondering why anyone would do it and what sort of implications it would have in C#. I’ve seen some information on other languages or for similar scenarios but not this specific scenario.

This is in a web service business rule.

public class MyClass
{
 StringBuilder strBuild = new StringBuilder();
 SQLText sqlQry = new SQLText();
 ...

 public myMethod1()
 {
   strBuild = new StringBuilder();
   sqlQry = new SQLText();
   ...
 }

 private myMethod2()
 {
   strBuild = new StringBuilder();
   sqlQry = new SQLText();
   ...
 }

 ...
}

If I was writing this I would not create class variables and would simply create local variables for the StringBuilder and SQLText types in each method I needed them. The above seems to be poor programming because it essentially creates a global variable that one has to remember to instantiate in each method or re-instantiate (to “clear” it).

But does the code above do any real harm?

  1. How does the garbage collector handle this? If the class variable is instantiated 30 times in different methods will it lose track of the initial declarations and fail to free the memory or would it still eventually get cleared?

  2. What about performance? Is it faster to use the same class variable(s) like this in a bunch of different methods or create a local variable in the respective methods?

  • 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-28T06:01:50+00:00Added an answer on May 28, 2026 at 6:01 am

    But does the code above do any real harm?

    Yes – it fouls up the state of the class. It looks like this class shouldn’t really have any state – these should be local variables. However, to the casual observer this appears to be real state associated with the object.

    1) How does the garbage collector handle this? If the class variable is instantiated 30 times in different methods will it lose track of the initial declarations and fail to free the memory or would it still eventually get cleared?

    Instance variables aren’t “instantiated” – they’re just assigned. And once the previous value has been overwritten, if nothing else refers to the object which the old value referred to, that object will be eligible for garbage collection. No harm there.

    2) What about performance? Is it faster to use the same class variable(s) like this in a bunch of different methods or create a local variable in the respective methods?

    I would expect local variables to behave slightly faster, if anything – but any performance difference is almost certainly going to be entirely insignificant (probably unmeasurably so), and shouldn’t be used to decide what to do.

    Just change them to local variables – clean up the design.

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

Sidebar

Related Questions

I am looking over someone Else's code and noticed that some of the user
I am looking over some code someone else did and I see this: if
I am looking into someone else's code and do not have much experience with
I was looking at some form validation code someone else had written and I
I am looking at some C# code written by someone else. Whenever a form
I am looking at someone else's vba excel code. they are doing ReDim Preserve
I was looking at someone else's code and noticed they called 'release' on an
I'm looking through someone else's code and am not sure what this means. It
Just learning php and looking into someone else's code. I'm not sure what is
I'm a beginner w/ Javascript. I'm looking at the following code that someone else

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.