c# - List <Class> New Class takes up memory? Do I need C=null; in the code? -


list new class takes memory?

do need c=null; in code below?

//class category public list<category> selectall() {   list<category> lv = new list<category>();     string command = "select * categories";   sqlcommand sc = new sqlcommand(command, new sqlconnection(globalfunction.function.getconnectionstring()));   sc.connection.open();   sqldatareader dr = sc.executereader();    using(dr)   {     while (dr.read())     {        // question cause 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);        // told add because if not cause memory leak.       c=null;                                                  }   }   sc.connection.close();   return lv; }  gridview1.datasource = list<category>;  gridview1.allowpaging = true; gridview1.pagesize = 5; gridview1.databind(); 

no, not need explicitly set c null.

the garbage collector determines if object can freed seeing if there outstanding references object. function selectall returns, reference object should in lv, lv.add(c). out of scope stack variable not cause additional reference, , not need set null.


Comments

Popular posts from this blog

c++ - How do I get a multi line tooltip in MFC -

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -