Is it alright to declare an object inside a foreach loop? My main method has a for loop that calls method1 that also has a for loop. I just want to change the setFileFolder value of the same object, is it ok to declare the same object again then change it’s value?
Sample code:
string[] arrs = { "a", "b", "c", "d", "e", "f", "g", "h", "i", };
foreach(string arr in arrs)
{
GetFile gf = new GetFile();
gf.setFileFolder(arr);
Console.WriteLine(gf.getFileFolder());
}
It is OK to declare an object inside a loop if this is what your intention is. In this case, each iteration of the loop will create a new instance of
GetFile.You can move the declaration outside the loop to reuse the same object: