Is this good practice? - Create an object instance in a loop
Posted on 2005-03-05
I am relatively new to C#, but have done a bit of Java in the past. I can not remember however if this is good practice?
I have this code
foreach(string fileName in fileEntries)
ArrayList fileDetailsArrayList = new ArrayList(); ***************
FileInfo fi = new FileInfo(fileName);
long fileSize = fi.Length;
//put the name and size into an ArrayList
//add the fileDetailsArrayList to the global Arraylist
My question is concerning the line marked with the ***'s. Is this good practice? Every time that chunk of code loops it will create a new ArrayList called fileDetailsArrayList, which works, but something in my mind tells me i'm causing unnessaccary memory usage or something.
If its all good, and C# handles it ok, then thats fine :-)