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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:53:06+00:00 2026-05-15T21:53:06+00:00

List New Class takes up memory? Do I need C=null; in the code below?

  • 0

List New Class takes up memory?

Do I need C=null; in the code below?

//class Category
public List<Category> SelectAll()
{
  List<Category> LV = new List<Category>();  
  string command = "SELECT * from categories";
  SqlCommand sc = new SqlCommand(command, new SqlConnection(GlobalFunction.Function.GetConnectionString()));
  sc.Connection.Open();
  SqlDataReader dr = sc.ExecuteReader();

  using(dr)
  {
    while (dr.Read())
    {

      // My Question is does this cause a memory problem...
      Category C = new Category(); 

      C.CategoryID = Convert.ToInt32(dr["CategoryID"]);
      C.Name = dr["Name"].ToString();
      C.DisplayOrder=Convert.ToInt32(dr["DisplayOrder"]);

      LV.Add(C);

      // I was told to add this because if not it would cause a memory leak.
      C=Null;                                            

    }
  }
  sc.Connection.Close();
  return LV;
}

GridView1.DataSource = List<Category>; 
GridView1.AllowPaging = true;
GridView1.PageSize = 5;
GridView1.DataBind();
  • 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-15T21:53:06+00:00Added an answer on May 15, 2026 at 9:53 pm

    No, you do not need to explicitly set C to null.

    The garbage collector determines if an object can be freed by seeing if there are any outstanding references to the object. As soon as the function SelectAll returns, the only reference to the object should be in LV, from LV.Add(C). The out of scope stack variable will not cause an additional reference, and does not need to be set to null.

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

Sidebar

Related Questions

class Program { static void Main(string[] args) { List<Book> books = new List<Book> {
I have this piece of code List<Class<? extends SubApplication>> appClasses = new ArrayList<Class<? extends
I've created two entities (simplified) in C#: class Log { entries = new List<Entry>();
List<CertMail> lsRecipetNumber = new List<CertMail>(); CertMail class is in data access layer which returns
Considering this part of a Java class, private List<Object> components = new ArrayList<Object>(); private
The following code takes a collection of class C and creates a collection consisting
Take a look at the following program: class Test { List<int> myList = new
I'd like to return a list through a web service that takes a string
Ok, so basically I need to create a generic method which takes a list
To read file into a list, create a new class called ListUtils and write

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.