In C #, is there an advantage or disadvantage to reinitializing a previously declared variable instead of declaring and initializing a new one? (Ignoring thoughts of man's conciseness and readability.)
For example, compare these two examples:
DataColumn col = new DataColumn();
col.ColumnName = "Subsite";
dataTable.Columns.Add(col);
col = new DataColumn();
col.ColumnName = "Library";
dataTable.Columns.Add(col);
vs
DataColumn col1 = new DataColumn();
col1.ColumnName = "Subsite";
gridDataTable.Columns.Add(col1);
DataColumn col2 = new DataColumn();
col2.ColumnName = "Library";
gridDataTable.Columns.Add(col2);
A similar example involving loops:
string str;
for (int i = 0; i < 100; i++)
{
str = "This is string #" + i.ToString();
Console.WriteLine(str);
}
vs
for (int i = 0; i < 100; i++)
{
string str = "This is string #" + i.ToString();
Console.WriteLine(str);
}
Edit: Thank you all for your answers. After reading them, I thought that I would expand my question a little:
Please correct me if I am wrong.
When I declare and initialize a reference type, such as System.String, I have a pointer to this object that exists on the stack, and the contents of the object that exists on the heap (accessible only through the pointer).
, "str", 100 String, . , , "str", String. "" , , , , .
, 100 100 String.
, , . , ; , , ? , , , , , 100 , .
, " - ", , .