Note that there are some explanatory texts on larger screens.

plurals
  1. PORe-initialize variable or declare new?
    text
    copied!<p>In C#, is there a benefit or drawback to re-initializing a previously declared variable instead of declaring and initializing a new one? (Ignoring thoughts on conciseness and human readability.)</p> <p>For example, compare these two samples:</p> <pre><code>DataColumn col = new DataColumn(); col.ColumnName = "Subsite"; dataTable.Columns.Add(col); col = new DataColumn(); // Re-use the "col" declaration. col.ColumnName = "Library"; dataTable.Columns.Add(col); </code></pre> <p>vs</p> <pre><code>DataColumn col1 = new DataColumn(); col1.ColumnName = "Subsite"; gridDataTable.Columns.Add(col1); DataColumn col2 = new DataColumn(); // Declare a new variable instead. col2.ColumnName = "Library"; gridDataTable.Columns.Add(col2); </code></pre> <p>A similar example involving loops:</p> <pre><code>string str; for (int i = 0; i &lt; 100; i++) { str = "This is string #" + i.ToString(); // Re-initialize the "str" variable. Console.WriteLine(str); } </code></pre> <p>vs</p> <pre><code>for (int i = 0; i &lt; 100; i++) { string str = "This is string #" + i.ToString(); // Declare a new "str" each iteration. Console.WriteLine(str); } </code></pre> <p><strong>Edit:</strong> Thank you all for your answers so far. After reading them, I thought I'd expand on my question a bit:</p> <p>Please correct me if I'm wrong. </p> <p>When I declare and initialize a reference type like a System.String, I have a pointer to that object, which exists on the stack, and the object's contents, which exist on the heap (only accessible through the pointer).</p> <p>In the first looping example, it seems like we create only one pointer, "str", and we create 100 instances of the String class, each of which exists on the heap. In my mind, as we iterate through the loop, we are merely changing the "str" pointer to point at a new instance of the String class each time. Those "old" strings that no longer have a pointer to them will be garbage collected--although I'm not sure when that would occur.</p> <p>In the second looping example, it seems like we create 100 pointers in addition to creating 100 instances of the String class.</p> <p>I'm not sure what happens to items on the stack that are no longer needed, though. I didn't think the garbage collector got rid of those items too; perhaps they are immediately removed from the stack as soon as you exit their scope? Even if that's true, I'd think that creating only one pointer and updating what it points to is more efficient than creating 100 different pointers, each pointing to a unique instance.</p> <p>I understand the "premature optimization is evil" argument, but I'm only trying to gain a deeper understanding of things, not optimize my programs to death.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload