I was working with some C# code today in the morning and I had something like:
foreach(DataRow row in MyMethod.GetDataTable().Rows) { //do something }
So, as I dont have a full understanding of the language framework I would like to know if GetDataTable() gets called each time an iteration is done or if it just gets called once and the resulting data (which would be Rows) is saved in memory to loop through it. In any case, I declared a new collection to save it and work from there…
I added a new variable so instead I did:
DataRowCollection rowCollection = MyMethod.GetDataTable().Rows; foreach(DataRow row in rowCollection) { //do something }
But im not quite sure if this is necessary.
Thanks in advance.
Don’t worry about it; it’ll only execute GetDataTable() once internally to get the enumerator object from the DataRowCollection, and then fetch a new item from it every run through the loop.